
var size = {x:0, y:0},
	pos = {x:0, y:0},
	pWidth = 800,
	pSpeed = 700,
	dropSpeed = 2000,
	dropSpeedS = 1000,
	fHeight = 56,
	fTop = 10,
	fBottom = 20,
	scrollHeight = 0,
	scrollHeightHidden = 0,
	scrollPosition = 0,
	scrollStep = 50,
	scrollContent = false,

	newsScrollStop = false,

	$mWindow,
	$mWindowInner,
	mWindow = {
		top: 0,
		left: 0
	},

	itemHoverSpeed = 200,
	newsSpeed = 10,

	itemIndex = 0,
	
	$shadow,
	$close,
	$logo,
	$menu,
	$content,
	$loader,
	$slider,

	preload = [
		'templates/vizan/images/p-window.jpg',
		'templates/vizan/images/p-window-bg.png',
		'templates/vizan/images/m-window.png',
		'templates/vizan/images/p-close.png',
		'templates/vizan/images/sprint-logo-corner.png'
	],

	opened = {
		name: '',
		code: '', // nie uzywane
		handle: null
	},
	
	secOpened = {
		name: '',
		code: '',
		handle: null
	},

	loader = {
		images: false,
		news: false
	},

	load,
	loadTimer;


function closeWindow(call) {
	if (opened.handle) {
		$menu.animate({opacity: 0}, pSpeed);
		$logo.animate({opacity: 0}, pSpeed);
		$content.css('opacity', 0);

		//removeSlider();

		logoIn();
		
		opened.handle.animate({
			width: size.x + 'px',
			height: size.y + 'px',
			top: pos.y + 'px',
			left: pos.x + 'px'
		}, {
			duration: pSpeed,
			easing: 'easeInBack',
			complete: function() {
				$close.remove();
				$(this)
				.attr('href', opened.code)
				.unbind('click')
				.click(function(e) {
					e.preventDefault();
					openWindow($(this));
				})
				.removeClass('p-window')
				.html(secOpened.name != '' ? secOpened.name : opened.name)
				.css({zIndex: 'auto'});

				if (secOpened.code != '') $(this).attr('href', secOpened.code);

				opened.handle.tips();

				//opened.handle.hoverItems();
				
				opened.handle = null;
				opened.code = '';

				secOpened.name = '';
				secOpened.code = '';
				secOpened.handle = null;
				
				closeShadow();

				if (call && $.isFunction(call)) call(this);
			}
		});
	}
}

function openWindow($this) {

	//removeHover($this);
	openShadow();

	logoOut();

	opened = {
		name: $this.html(),
		code: $this.attr('href'),
		handle: $this
	};

	$this.detip();

	var code = $this.attr('href');
	
	size.y = $this.height().toInt();
	size.x = $this.width().toInt();
	pos.y = $this.css('top').toInt();
	pos.x = $this.css('left').toInt();
	
	var tTop = $('#thumbs').position().top.toInt();
	var tWidth = $('#thumbs').outerWidth().toInt();
	var height = $(document).height().toInt();
	
	$this
	.stop()
	.unbind('click')
	.attr('href', 'javascript:void(0)')
	.css({
		zIndex: 1000
	})
	.text('')
	.addClass('p-window')
	.animate({
		width: pWidth + 'px',
		height: (height - fTop - fHeight - fBottom) + 'px',
		left: -((pWidth - tWidth) / 2) + 'px',
		top: -(tTop - fTop) + 'px'
	}, {
		duration: pSpeed,
		easing: 'easeOutBack',
		complete: function() {
			loadClose();
			loadMenu(code);
			loadContent();
		}
	});
}

function loadSlider($this, $conn) {

	if ($slider) $slider.remove();

	scrollHeightHidden = $this.outerHeight().toInt();

	if (scrollHeight <= scrollHeightHidden) return false;

	scrollPosition = 0;

	$slider = $('<div></div>')
	.css({
		height: scrollHeightHidden - 35 + 'px',
		opacity: 0
	})
	.slider({
		orientation: 'vertical',
		animate: true,
		min: 0,
		max: scrollHeight - scrollHeightHidden,
		value: scrollHeight - scrollHeightHidden,
		slide: function(e, ui) {
			scrollPosition = scrollHeight - scrollHeightHidden - ui.value;
			$this.scrollTop(scrollPosition);
		}
	})
	.addClass('p-knob-area')
	.prependTo($conn)
	.animate({opacity: 1});

	$this
	.wheel(function(e, delta) {

		if ($.browser.opera) {
			scrollPosition = delta > 0 ? scrollPosition + scrollStep : scrollPosition - scrollStep;
		} else {
			scrollPosition = delta > 0 ? scrollPosition - scrollStep : scrollPosition + scrollStep;
		}

		if (scrollPosition <= 0) {
			scrollPosition = 0;
		} else if (scrollPosition >= (scrollHeight - scrollHeightHidden)) {
			scrollPosition = scrollHeight - scrollHeightHidden;
		}

		$slider.slider('value', scrollHeight - scrollHeightHidden - scrollPosition);
		$this.scrollTop(scrollPosition);
	});

	$(window).wheel(function(e) {
		if (scrollContent) e.preventDefault();
	});
}

function removeSlider() {
	if ($slider) {
		$slider
		.css('opacity', 1)
		.animate({opacity: 0}, {
			duration: pSpeed,
			complete: function() {
				$(this).remove();
			}
		});
		$('a', $slider)
		.css('opacity', 1)
		.animate({opacity: 0}, {
			duration: pSpeed,
			complete: function() {
				$(this).remove();
			}
		});
	}
}

function setScrollHeight($this) {
	scrollHeight = $this.outerHeight();
}

function loadMenu(code) {

	$logo = $('<div></div>')
			.attr('id', 'p-vizan')
			.text(opened.name)
			.appendTo(opened.handle);
			
	$menu = $('<div></div>')
			.attr('id', 'p-menu')
			.load('ajax/CC_Products.aMenu',{
				rand: Math.random(),
				code: code
			}, function() {
				
				relocateItems($('#menu'), 21, 1, 500);

				moveItems($('.anim-s'));

				$('.item-s')
				.data('code', code)
				/*
				.mouseenter(function() {
					if ($(this).not('a:animated').length > 0) {
						var s = 10;
						$(this)
						.animate({left: '-=5px'}, {duration: s})
						.animate({left: '+=10px'}, {duration: s})
						.animate({left: '-=10px'}, {duration: s})
						.animate({left: '+=5px'}, {duration: s})

						.animate({left: '-=5px'}, {duration: s})
						.animate({left: '+=10px'}, {duration: s})
						.animate({left: '-=10px'}, {duration: s})
						.animate({left: '+=5px'}, {duration: s})
					}
				})
				*/
				.click(function(e) {
					e.preventDefault();
					ajax($(this));
					/*
					if ($(this).attr('href').contains('http') < 0) {
						e.preventDefault();
						ajax($(this));
					} else {
						$.Go($(this).attr('href'));
					}
					*/
				})
				.focus(function() {
					this.blur();
				})
				.each(function() {
					$(this).addClass('item-s' + $(this).attr('rel').toInt());
				});

				$('#menu-close', $menu)
				.unbind('click')
				.click(function(e) {
					e.preventDefault();
					var length = moveItems($('.anim-s'), -500, 'easeInBack');

					var timer = setInterval(function() {
						if (itemIndex >= length) {
							clearInterval(timer);
							closeWindow();
						}
					}, 10);
				});

				ajax($('#menu a.item-s:first'));
			})
			.appendTo(opened.handle);
}

function selectItem($this) {
	if (!$this.attr('rel')) return;
	$('.item-s').each(function() {
		var size = $(this).attr('rel').toInt();
		$(this).removeClass('item-sw' + size);
	});
	$this.addClass('item-sw' + $this.attr('rel').toInt());
}

function loadContent() {
	$loader = $('<div></div>')
				.attr('id', 'p-loader')
				.appendTo(opened.handle);
	$content = $('<div></div>')
				.attr('id', 'p-content')
				.mouseenter(function() {
					scrollContent = true;
				})
				.mouseleave(function() {
					scrollContent = false;
				})
				.appendTo($loader);
}

function ajax($this) {

	//removeSlider();
	selectItem($this);
	
	$loader.addClass('p-loader');

	$content.css('opacity', 0);
	//$content.animate({opacity: 0}, {
		//complete: function() {
			
			var href = $this.attr('href') ? $this.attr('href') : '';
			var type = href.substr(href.lastIndexOf('/') + 1);

			if (type == 'gallery') {
				$loader.removeClass('p-loader');
				$content.html('').css('opacity', 1);
				$('<div></div>').attr('id', 'photos-grid').appendTo($content).photosGrid($this.data('code'));
			} else {
				$.ajax({
					url: 'ajax/CC_Products.aOpen',
					type: 'post',
					data: {
						code: $this.data('code'),
						type: type
					},
					dataType: 'html',
					success: function(data) {
						$loader.removeClass('p-loader');
						/*$content
						.css({
							'overflow': 'visible',
							'height': 'auto'
						});*/
						$content.css('opacity', 1);

						if (type == 'presentation' && false) {
							$content.html('');
							startSlideshow($content, $this.data('code'));
						} else {
							$content.html(data);
						}

						//setScrollHeight($content);

						$content
						.css({
							'opacity': 1,
							'height': '97%'
						});

						//loadSlider($content, opened.handle);

						startFancy();
					}
				});
			}
		//}
	//});
}


jQuery.fn.handleButton = (function($) {
	return function() {
		return this.each(function() {
			$(this)
			.addClass('ui-state-default ui-corner-all')
			.hover(
				function() {
					$(this).toggleClass('ui-state-hover');
				},
				function() {
					$(this).toggleClass('ui-state-hover');
				}
			);
		});
	};
})(jQuery);


function startSlideshow($content, code) {

	var slideshow = $('<div></div>')
	.attr('id', 'slideshow')
	.appendTo($content);

	var buttons = $('<div></div>')
	.attr('id', 'buttons')
	.appendTo($content);

	buttons
	.css({
		opacity: 0
	})
	.html('');

	slideshow
	.css({
		width: 550
	})
	.html('');

	var con = $('<div></div>');
	con.css({
		opacity: 0
	});
	con.attr('#con');
	con.appendTo(slideshow);

	$.aPostHTML({
		url: 'ajax/CC_Products.aGetSlideshowImagesByCode',
		data: {
			code: code
		},
		success: function(data) {

			var files = data.split('|'),
			length = files.length,
			preloaded = 0,
			height = 0,
			width = 0,
			loaded = false,
			active = 0;
			
			for(var i = 0; i < files.length; i++ ) {

				var img = new Image();
				$(img)
				.load(function() {
					preloaded++;
					var h = $(this).height();
					if (h > height) {
						slideshow.height(h);
						height = h;
					}
					width += $(this).width();
					if (loaded == false && preloaded == length) loaded = true;
				})
				.addClass('slideshow-image')
				.attr('src', files[i])
				.attr('alt', files[i])
				.appendTo(con);
			}

			var timer = setInterval(function() {
				if (loaded == true) {
					timer = clearInterval(timer);

					con.width(width + 100);

					$('<input/>')
					.attr('type', 'button')
					.click(function() {
						active--;
						if (active < 0) active++;
						slideshow.scrollLeft(active * 550);
					})
					.val(Lang.slideshow.prev)
					.handleButton()
					.appendTo(buttons);

					$('<input/>')
					.attr('type', 'button')
					.click(function() {
						active++;
						if (active >= length) active--;
						slideshow.scrollLeft(active * 550);
					})
					.val(Lang.slideshow.next)
					.handleButton()
					.appendTo(buttons);

					buttons.animate({
						opacity: 1
					});

					con.animate({
						opacity: 1
					});
				}
			}, 10);
		}
	});
}

function loadClose($h, ex) {

	var $handle = $h || opened.handle;
	var exec = ex || closeWindow;

	$close = $('<a href=""></a>')
			.attr('title', Lang.closeWindow)
			.click(function(e) {
				e.preventDefault();
				
				$(this).animate({
					opacity: 0
				}, {
					complete: function() {
						var length = moveItems($('.anim-s'), -500, 'easeInBack');

						var timer = setInterval(function() {
							if (itemIndex >= length) {
								clearInterval(timer);
								exec();
							}
						}, 10);
					}
				});
			})
			.focus(function() {
				this.blur();
			})
			.tips()
			.addClass('p-close')
			.prependTo($handle)
			.css({opacity: 0})
			.animate({
				opacity: 1
			});
}

function openShadow() {
	closeShadow();
	
	$shadow = $('<div></div>')
				.addClass('p-window-bg')
				.css({
					width: $('body').outerWidth().toInt() + 'px',
					height: $('body').outerHeight().toInt() - fHeight + 'px',
					left: - $('#thumbs').offset().left + 'px',
					top: - $('#thumbs').offset().top + 'px'
				})
				.click(function() {
					$close.animate({
						opacity: 0
					}, {
						complete: function() {
							var length = moveItems($('.anim-s'), -500, 'easeInBack');

							var timer = setInterval(function() {
								if (itemIndex >= length) {
									clearInterval(timer);
									closeWindow();
									closeModal();
								}
							}, 10);
						}
					});
				})
				.attr('title', Lang.clickToCloseWindow)
				.prependTo('#thumbs');

	$(window).resize(positionShadow);
}

function positionShadow() {

	$shadow.css({
		width: $('body').outerWidth().toInt() + 'px',
		height: $('body').outerHeight().toInt() - fHeight + 'px',
		left: - $('#thumbs').offset().left + 'px',
		top: - $('#thumbs').offset().top + 'px'
	});


}

function closeShadow() {
	if ($shadow) {
		$shadow.remove();
		$(window).unbind('resize', positionShadow);
	}
}

function tremble() {
	var s = 20;
	
	$('.anim:not(.item)').each(function() {
		$(this).mouseenter(function() {
			if ($(this).not('a:animated').length > 0) {
				$(this)
				.animate({left: '-=5px'}, {duration: s})
				.animate({left: '+=10px'}, {duration: s})
				.animate({left: '-=5px'}, {duration: s})
				.animate({top: '-=5px'}, {duration: s})
				.animate({top: '+=10px'}, {duration: s})
				.animate({top: '-=5px'}, {duration: s});
				/*
				.animate({
					marginTop: '-=2.5px',
					marginLeft: '-=2.5px',
					height: '+=5px',
					width: '+=5px'
				}, {duration: s})
				.animate({
					marginTop: '+=2.5px',
					marginLeft: '+=2.5px',
					height: '-=5px',
					width: '-=5px'
				}, {duration: s})
				*/
			}
		});
	});
}

function relocateItems(conn, s, m, mt) {
	var left = 0,
		top = 0,
		tWidth = conn.width().toInt(),
		size = s || 43,
		space = m || 5,
		mTop = mt || 1000;

	conn.children().each(function() {
		var rel = $(this).attr('rel');
		var width = rel && rel != 'undefined' && rel.toInt() > 0 ? (rel.toInt() * size + ((rel.toInt() - 1) * space)) : size;

		$(this).css({
			'width': width + 'px',
			'left': left + 'px',
			'top': top + 'px',
			'marginTop': -mTop + 'px'
		});

		left += (space + width);

		if (left >= tWidth) {
			left = 0;
			top += (space + size);
		}
	});
}



function moveItems($items, m, e) {

	var margin = m || 0,
		easing = e || 'jswing';

	itemIndex = 0;

	$items.each(function() {
		var duration = Math.ceil(Math.random() * dropSpeed);
		$(this).animate({
			'marginTop': margin + 'px'
		}, {
			duration: duration,
			easing: easing,
			complete: function() {
				itemIndex++;
			}
		});
	});
	
	return $items.length;
}

function openModal(e, url, data, call) {

	if ($mWindow) {
		closeModal(function() {
			openModal(e, url, data, call);
		});
		return false;
	} else if (opened.handle) {
		if ($close) $close.animate({opacity: 0});
		var length = moveItems($('.anim-s'), -500, 'easeInBack');

		var timer = setInterval(function() {
			if (itemIndex >= length) {
				clearInterval(timer);
				closeWindow(function() {
					modalOpenAnimation(e, url, data, call);
				});
			}
		}, 10);
	} else {
		modalOpenAnimation(e, url, data, call);
	}
}

function modalOpenAnimation(e, url, data, call) {
	
	var $thumbs = $('#thumbs');

	if (!e) {
		var e = {
			pageY: 0,
			pageX: 0
		};
	}

	openShadow();

	mWindow = {
		top: e.pageY - $thumbs.offset().top,
		left: e.pageX - $thumbs.offset().left
	};

	$mWindow = $('<div></div>')
	.css({
		top:  mWindow.top + 'px',
		left: mWindow.left + 'px'
	})
	.addClass('m-window')
	.prependTo($thumbs)
	.animate({
		width: $thumbs.outerWidth() + 'px',
		height: $thumbs.outerHeight() + 'px',
		top: '0px',
		left: '0px'
	}, {
		easing: 'easeOutBack',
		complete: function() {

			$mWindowInner = $('<div></div>')
			.addClass('m-inner')
			.css({
				opacity: 0
			})
			.appendTo($mWindow)
			.load(url, data, function(t) {

				//setScrollHeight($mWindowInner);

				$(this).css('opacity', 1);

				startFancy();

				loadClose($mWindow, closeModal);
				//if (url.contains('Contact') < 0) loadSlider($mWindowInner, $mWindow);

				if (call && $.isFunction(call)) call(this);
			});
		}
	});
}

function closeModal(call) {
	$mWindow
	.children()
	.animate({
		opacity: 0
	});

	if ($slider) $slider.remove();

	$mWindow.animate({
		width: '0px',
		height: '0px',
		top: mWindow.top + 'px',
		left: mWindow.left + 'px'
	}, {
		easing: 'easeInBack',
		complete: function() {
			$mWindow.remove();
			closeShadow();
			$mWindow = null;
			if (call && $.isFunction(call)) call(this);
		}
	});
}

function modalCloseAnimation(call) {
	
	
}

function inputCSS() {
	// Automatycznie klasy inputów
	$('button, input[type=button], input[type=submit], input[type=reset]')
	.addClass('ui-state-default ui-corner-all')
	.hover(
		function() {
			$(this).toggleClass('ui-state-hover');
		},
		function() {
			$(this).toggleClass('ui-state-hover');
		}
	);

	// Automatycznie klasy pol tekstowych
	$('input[type=text], input[type=password], textarea, select').addClass('text ui-widget-content ui-corner-all');
}

jQuery.fn.hoverItems = (function($) {
	return function() {
		return this.each(function() {
			if ($(this).data('hover_color')) $(this).css('color', $(this).data('hover_color'));
			$(this)
			.mouseenter(function() {
				if (!$(this).data('hover_color')) $(this).data('hover_color', $(this).css('color'));
				$(this).animate({
					color: '#1d5299'
				}, itemHoverSpeed);
			}).mouseleave(function() {
				$(this).animate({
					color: $(this).data('hover_color')
				}, itemHoverSpeed);
			});
		});
	};
})(jQuery);

function removeHover($items) {
	$items
	.unbind('mouseenter mouseleave')
	.css('color', '#868585');
}

function initNews() {

	var $conn = $('#news-slider #center');
	var $news = $('div', $conn);
	var width = $conn.width();

	$news
	.css({
		opacity: 0,
		merginLeft: width + 'px'
	})
	.load('ajax/CC_News.aInitNews', {
		rand: Math.random()
	}, function() {
		var scrollWidth = $conn.scrollWidth(),
			i = width;

		$news
		.css({
			width: '100000px',
			opacity: 1
		})
		.hover(function() {
			newsScrollStop = true;
		}, function() {
			newsScrollStop = false;
		});

		var speed =  newsSpeed / ($.browser.msie ? 10 : 1);

		setInterval(function() {
			if (newsScrollStop==false) {
				if (i < -scrollWidth) i = width;
				i -= $.browser.msie ? 2 : 1;
				$news.css('margin-left', i);
			}
		}, newsSpeed);
		
		$('a', $news).focus(function() {
			this.blur();
		})
		.click(function(e) {
			e.preventDefault();
			var href = $(this).attr('href');
			openModal(e, 'ajax/CC_News.mView', {
				id: href.substr(href.lastIndexOf('/') + 1)
			});
		})
		.hoverItems();

		var t = setInterval(function() {
			t = clearInterval(t);
			loader.news = true;
			$conn.css({
				opacity: 1
			});
		}, 500);
	});
}

function runRssNews() {
	var href = document.location.href;
	var id = href.substr(href.lastIndexOf('/') + 1);

	if (id > 0) {
		openModal(null, 'ajax/CC_News.mView', {
			id: id
		});
	}
}

function runProduct() {
	var href = document.location.href;
	var lastChar = href.charAt(href.length - 1);
	if (lastChar == '/') href = href.substr(0, href.length - 1);
	var code = href.substr(href.lastIndexOf('/') + 1);
	code = code.indexOf('.') > -1 ? code.substr(0, code.indexOf('.')) : code;
	
	if (code != '') {
		if ($('a.item[href$=' + code + ']').length) {
			$('a.item[href$=' + code + ']').click();
		} else {
			$('#logo[href$=' + code + ']').click();
		}
	}
}

function logoIn() {
	$('#logo').css('top', '-500px').animate({top: '20px'});
}

function logoOut() {
	$('#logo').animate({top: '-500px'}, {
		easing: 'easeInBack'
	});
}

function loadImages() {
	
	var length = preload.length,
		preloaded = 0;

	$.each(preload, function(i, src) {
		var img = new Image();
		$(img)
		.load(function() {
			preloaded++;
			if (loader.images == false && preloaded == length) loader.images = true;
		})
		.attr('src', src);
	});
}

function removeLytebox() {
	$('#lbOverlay, #lbMain').remove();
}

function startFancy() {
	$('a.popup').fancybox({
		imageScale: true,
		overlayOpacity: .8,
		overlayShow: true,
		enableEscapeButton: true,
		hideOnOverlayClick: true,
		hideOnContentClick: false,
		centerOnScroll: true
	});
}

function stopFancy() {
	
}


function pageLoader() {
	load = $('<div></div>')
	.attr('id', 'page-loader')
	.css({
		'height': $(document.html).height() + 'px'
	})
	.appendTo('body');
}


function holdVertical() {

	var $window = $(window);
	var $main = $('#main');
	var $site = $('#site');
	var $footer = $('#footer');
	
	fHeight = $footer.outerHeight().toInt();
	var outerHeight = $site.outerHeight() + 70;

	var mainHeight = $window.height() - fHeight - $main.InOutDiff();
	if (mainHeight < outerHeight) mainHeight = outerHeight;

	$main.css('height', mainHeight + 'px');
	$site.css('margin-top', ((mainHeight / 2) - ($site.height() / 2) - 70) + 'px');
}










jQuery.fn.photosGrid = (function() {

	return function(code) {


		return this.each(function() {

			var $this = $(this),
				page = 1,
				length = 0,
				preloaded = 0,
				loaded = false,
				groupName = 'photos-' + Math.ceil(Math.random() * 1000);


			$this.children().remove();

			var putPhoto = function(filepath, description) {
				
				$con = $('<div></div>')
				.hide()
				.appendTo($this);


				$a = $('<a></a>')
				.attr('href', '')
				.attr('rel', groupName)
				.addClass('photo')
				.click(function(e) {
					e.preventDefault();
				})
				.focus(function() {
					this.blur();
				})
				.css('cursor', 'default')
				.appendTo($con);


				$img = $('<img />')
				.load(function() {
					preloaded++;
					if (loaded == false && preloaded == length) loaded = true;

					var $_a = $(this).data('imageAnchor');

					$_a
					.attr('href', $(this).attr('src').replace('/t_', '/'))
					.unbind('click')
					.css('cursor', 'pointer');

					$(this)
					.css('opacity', 0)
					.appendTo($_a)
					.fadeTo('slow', .8, function() {
						$_a.css('background-image', 'none');
						$(this)
						.mouseover(function() {
							$(this).fadeTo('fast', 1);
						})
						.mouseout(function() {
							$(this).fadeTo('fast', .8);
						});
					});
				})
				.data('imageAnchor', $a)
				.attr('src', filepath)
				.attr('alt', description || '');

				$con.fadeIn('slow');
			};

			var timer = setInterval(function() {
				if (loaded == true) {
					clearInterval(timer);
					
					loadSlider($content, opened.handle);

					$('a.photo', $this).fancybox({
						imageScale: true,
						overlayOpacity: .8,
						overlayShow: true,
						titlePosition: 'over',
						autoScale: true,
						transitionIn: 'none',
						transitionOut: 'none',
						enableEscapeButton: true,
						hideOnOverlayClick: true,
						hideOnContentClick: false,
						centerOnScroll: true
					});
				}
			}, 100);

			$.aPostJSON({
				url: 'ajax/CC_Products.aGetPhotosGrid',
				data: {
					p_code: code,
					page: page
				},
				success: function(json) {

					length = $.objSize(json);
					$.each(json, function(pp_id, data) {
						var _data = data.split(']|[');
						putPhoto(_data[0] || '', _data[1] || '');
					});
				}
			});
		});
	};
})(jQuery);








$(function() {

	pageLoader();


	$(document).pngFix();

	loadImages();
	initNews();

	// Chmurki
	$('a').tips().focus(function() {
		$(this).blur();
	});
	
	/*
	$('.item')
	.each(function() {
		$(this).data('hover_color', $(this).css('color'));
	});
	*/

	inputCSS();

	// Zmiana języka
	$('#l-pl, #l-en').click(function(e) {
		e.preventDefault();
		
		var href = $(this).attr('href');

		var length = moveItems($('.anim'), -1000, 'easeInBack');
		$('#logo').animate({
			top: '-500px'
		}, {
			easing: 'easeInBack'
		});
		$('#choose').css('visibility', 'hidden');

		var timer = setInterval(function() {
			if (itemIndex >= length) {
				timer = clearInterval(timer);
				pageLoader();
				$.Go(href);
			}
		}, 100);
	})
	.focus(function() {
		this.blur();
	});

	$('#contact')
	.click(function(e) {
		e.preventDefault();
		openModal(e, 'ajax/CC_Contact.mForm', {
			rand: Math.random()
		}, function($this) {
			inputCSS();
			$.each(Lang.Contact, function(id, text) {
				$this = $('#' + id);
				if ($this.attr('value')) {
					$this.val(text);
				} else {
					$this.text(text);
				}
			});

			var $form = $('#fContact');
			$('#p_id', $form).change(function() {
				$('td.contact-data div').hide();
				$('#CP-' + $(this).val(), $form).show();
			});
			$form.send();
		});
	})
	.focus(function() {
		this.blur();
	});

	//$('#choose').css({opacity: 0});

	holdVertical();

	$(window).resize(function() {
		holdVertical();
	});

	relocateItems($('#thumbs'), 43, 5, 1000);


	$('#logo').click(function(e) {
		e.preventDefault();
		$('#vizan-start').click();
	});
	$('.item').click(function(e) {
		e.preventDefault();
		openWindow($(this));
	})
	.focus(function() {
		this.blur();
	})
	.each(function() {
		$(this).addClass('item' + $(this).attr('rel'));
	});

	/*swfobject.embedSWF("index.swf", "news-flash", "100%", "28", "9.0.0", '', {}, {
		wmode: 'transparent'
	});*/
});

$(window).load(function() {


	loadTimer = setInterval(function() {
		//if (loader.images && loader.news) {
		if (loader.images) {
			loadTimer = clearInterval(loadTimer);
			load.animate({
				opacity: 0
			}, {
				complete: function() {
					$(this).remove();

					logoIn();

					var length = moveItems($('.anim'));

					var timer = setInterval(function() {
						if (itemIndex >= length) {
							timer = clearInterval(timer);
							//$('#choose').animate({opacity: 1});
							//tremble();
							runRssNews();
							runProduct();
						}
					}, 100);
				}
			})
		}
	}, 10);
});


