
var size = {x:0, y:0},
	pos = {x:0, y:0},
	pWidth = 800,
	pSpeed = 700,
	uniWidth = 800,
	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) {
		if ($menu) $menu.animate({opacity: 0}, pSpeed);
		if ($logo) $logo.animate({opacity: 0}, pSpeed);
		$content
		.css({
			overflow: 'hidden',
			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() {

				$(this).remove();

				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();

	size = {
		x: $this.width().toInt(),
		y: $this.height().toInt()
	};

	pos = {
		x: $this.offset().left.toInt(),
		y: $this.offset().top.toInt()
	};
	
	opened = {
		name: $this.text(),
		code: $this.attr('href'),
		handle: $('<div></div>')
				.addClass('p-window')
				.appendTo(document.body)
				.css({
					top: pos.y + 'px',
					left: pos.x + 'px',
					width: size.x + 'px',
					height: size.y + 'px'
				})
				.animate({
					width: pWidth + 'px',
					height: ($(document).height().toInt() - fHeight - 20) + 'px',
					left: ($(document).width().toInt() / 2) - (pWidth / 2) + 'px',
					top: 10 + 'px'
				}, {
					duration: pSpeed,
					easing: 'easeOutBack',
					complete: function() {
						loadClose();
						loadMenu(opened.code);
						loadContent();
					}
				})
	};
}

function openUni(url, exec, title) {

	openShadow();

	opened = {
		code: 'uni',
		handle: $('<div></div>')
				.addClass('uni-window')
				.css({
					top: pos.y + 'px',
					left: pos.x + 'px',
					width: size.x + 'px',
					height: size.y + 'px'
				})
				.animate({
					width: uniWidth + 'px',
					height: ($(document).height().toInt() - fHeight - 20 - 12) + 'px',
					left: ($(document).width().toInt() / 2) - (uniWidth / 2) + 'px',
					top: 10 + 'px'
				}, {
					duration: pSpeed,
					easing: 'easeOutBack',
					complete: function() {

						loadClose();

						$loader = $('<div></div>')
									.attr('id', 'p-loader')
									.addClass('p-loader')
									.css('margin', 'auto')
									.appendTo(opened.handle);
						$content = $('<div></div>')
									.attr('id', 'p-content')
									.css('opacity', 0)
									.mouseenter(function() {
										scrollContent = true;
									})
									.mouseleave(function() {
										scrollContent = false;
									})
									.appendTo($loader);
									
						$content.load(url, null, function() {
							$loader.removeClass('p-loader');

							if (title && title != '') {
								$('<h2></h2>')
								.addClass('uni-title')
								.text(title)
								.prependTo($content);
							}

							if (exec) exec($content);

							$content.css('opacity', 1);
						});
					}
				})
				.appendTo(document.body)
	};
}

function openShadow() {

	closeShadow();

	$shadow = $('<div></div>')
				.addClass('p-window-bg')
				.css({
					width: $(document).width().toInt() + 'px',
					height: $(document).height().toInt() - fHeight + '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)
				.appendTo(document.body);

	$(window).resize(positionShadow);
}

function positionShadow() {
	$shadow.css({
		width: $(document).width().toInt() + 'px',
		height: $(document).height().toInt() - fHeight + 'px'
	});
}

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

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 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', $menu), 21, 1, 500);

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

				$('.item-s')
				.data('code', code)
				.click(function(e) {
					e.preventDefault();
					ajax($(this));
				})
				.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');
					
					$close.animate({
						opacity: 0
					}, {
						complete: function() {
							var timer = setInterval(function() {
								if (itemIndex >= length) {
									clearInterval(timer);
									closeWindow();
								}
							}, 10);
						}
					});
				});

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

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();
					}
				});
			}
		//}
	//});
}

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 selectItem($this) {
	if (!$this.attr('rel')) return false;
	$('.item-s').each(function() {
		var size = $(this).attr('rel').toInt();
		$(this).removeClass('item-sw' + size);
	});
	$this.addClass('item-sw' + $this.attr('rel').toInt());
}


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 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 = parseInt(conn.width()),
		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({
			'left': left + 'px',
			'top': top + 'px',
			'marginTop': -mTop + 'px'
		});
		if (!$(this).hasClass('spacer')) $(this).css('width', width + 'px');

		left += (space + width);

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

	conn.data('height', top + 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, height, width) {
	//alert('open modal');
	if ($mWindow && $mWindow !== null) {
		//alert('close modal');
		closeModal(function() {
			//alert('reopen modal');
			openModal(e, url, data, call, height, width);
		});
	} else if (opened.handle !== null) {
		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, height, width);
				});
			}
		}, 10);
	} else {
		//alert('last open');
		modalOpenAnimation(e, url, data, call, height, width);
	}
}

function modalOpenAnimation(e, url, data, call, height, width) {

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

	openShadow();

	$mWindow = $('<div></div>')
	.addClass('m-window')
	.appendTo(document.body);
	//alert(height);
	$mWindow
	.css({
		width: '0px',
		height: '0px',
		top: e.pageY + 'px',
		left: e.pageX + 'px'
	})
	.animate({
		height: (height || 400) + 'px',
		width: (width || 670) + 'px',
		top: (($(document).height().toInt() - fHeight) / 2 - (height || 400) / 2) + 'px',
		left: (($(document).width().toInt() / 2) - ((width || 670) / 2)) + 'px'
	}, {
		easing: 'easeOutBack',
		complete: function() {
//alert('modal showed');
			$mWindowInner = $('<div></div>')
			.addClass('m-inner')
			.css({
				height: (height || 400) - 20 + 'px',
				width: (width || 670) - 15 + 'px'
			})
			.css({
				opacity: 0
			})
			.appendTo($mWindow)
			.load(url, data, function(t) {
//alert('content loaded');
				//setScrollHeight($mWindowInner);

				$(this).animate({
					opacity: 1
				});

				startFancy();

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

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

function closeModal(call) {
	
	if ($slider) $slider.remove();

	$mWindow
	.children()
	.animate({
		opacity: 0
	}, {
		complete: function() {
			$mWindow.animate({
				width: '0px',
				height: '0px',
				top: ($(document).height().toInt() / 2) - fHeight + 'px',
				left: ($(document).width().toInt() / 2) + 'px'
			}, {
				easing: 'easeInBack',
				complete: function() {
					if ($mWindow) $mWindow.remove();
					closeShadow();
					$mWindow = null;
					if (call && call !== null) {
						call(this);
						//call = null;
					}
				}
			});
		}
	});
}

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)
		.hoverItems()
		;//.newsLinks();

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

jQuery.fn.newsLinks = (function($) {
	return function() {
		return this.each(function() {
			$(this)
			.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)
				}, null, $(document).height() - fHeight - 20);
			});
		});
	};
})(jQuery);

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).toLowerCase();
	
	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)
		.css({
			position: 'absolute',
			width: '0px',
			height: '0px',
			opacity: 0
		})
		.prependTo(document.body)
		.load(function() {
			preloaded++;
			if (loader.images == false && preloaded == length) loader.images = true;
		})
		.attr('src', webPath + 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),
	$main = $('#main'),
	$site = $('#site'),
	$footer = $('#footer'),
	$tHeight = $('#thumbs'),
	$tDevHeight = $('#thumbs-dev'),
	height = parseInt($tHeight.data('height'));

	height = parseInt($tDevHeight.data('height')) > height ? parseInt($tDevHeight.data('height')) : height;
	
	fHeight = $footer.outerHeight().toInt();
	var outerHeight = $site.outerHeight();

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

	var marginTop = (mainHeight / 2) - (height / 2);

	$main.height(mainHeight + 'px');
	$site.css('margin-top', marginTop + 'px');

	if (opened.code != '') {
		if (opened.code == 'uni') {
			opened.handle.css({
				height: ($(document).height().toInt() - fHeight - 20 - 12) + 'px',
				top: 10 + 'px',
				left: ($(document).width().toInt() / 2) - (uniWidth / 2) + 'px'
			});
		} else {
			opened.handle.css({
				height: ($(document).height().toInt() - fHeight - 20) + 'px',
				top: 10 + 'px',
				left: ($(document).width().toInt() / 2) - (pWidth / 2) + '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 bgLandLoop() {
	$('.item-land').each(function() {
		var $this = $(this),
		size = parseInt($this.attr('rel'));

		$this
		.css('background-position', '0px 0px')
		.animate({
			backgroundPosition: '-' + ((size * 43) + ((size - 1) * 5)) + 'px 0px'
		}, {
			queue: false,
			duration: 10000,
			easing: 'linear',
			complete: function() {
				bgLandLoop();
			}
		});
	});
}

function bgSeaLoop() {
	$('.item-sea').each(function() {
		var $this = $(this),
		size = parseInt($this.attr('rel'));

		$this
		.css('background-position', '0px 0px')
		.animate({
			backgroundPosition: '-' + ((size * 43) + ((size - 1) * 5)) + 'px 0px'
		}, {
			queue: false,
			duration: 10000,
			easing: 'linear',
			complete: function() {
				bgSeaLoop();
			}
		});
	});
}

function showFooterPop(file, url) {
	
	$('#footer-pop')
	.attr('href', url || 'javascript:void(0)')
	.css('cursor', url ? 'pointer' : 'default')
	.load(webPath + 'templates/vizan/html/' + file + '.phtml')
	.animate({
		opacity: 1
	});
}

function hideFooterPop() {
	$('#footer-pop').animate({
		opacity: 0
	})
}






$(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
	$('a.lang-change').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() {
			inputCSS();
			$.each(Lang.Contact, function(id, text) {
				var $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();
	});

	$('#news')
	.click(function(e) {
		e.preventDefault();

		size = {
			x: $this.width().toInt(),
			y: $this.height().toInt()
		};

		pos = {
			x: $this.offset().left.toInt(),
			y: $this.offset().top.toInt()
		};

		openModal(e, 'ajax/CC_News.aList', null, function($c) {
			$('<h2></h2>')
			.addClass('modal-title')
			.text('Latest news')
			.prependTo($c);

			$('a.title', $c).newsLinks();
		}, $(document).height() - fHeight - 20);
	})
	.focus(function() {
		this.blur();
	});


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

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

	holdVertical();

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


	$('#logo').click(function(e) {
		e.preventDefault();
		$('#vizan-start').click();
	});

	$('.item')
	.focus(function() {
		this.blur();
	})
	.each(function() {
		$(this).addClass('item' + $(this).attr('rel'));
	})
	.filter(function() {
		return !$(this).hasClass('no-click');
	})
	.click(function(e) {
		e.preventDefault();
		openWindow($(this));
	});

	$('#land, #sea')
	.data('clicked', 0)
	.click(function(e) {
		e.preventDefault();
		
		var $this = $(this);

		$this.data('clicked', $this.data('clicked') == 1 ? 0 : 1);

		var clicked = $this.data('clicked') == 1,
		type = $this.attr('id');

		if (clicked) {
			$this.addClass('active');

			if (type == 'land') {
				$('#sea')
				.removeClass('active')
				.data('clicked', 0);

				moveItems($('a.land'));
				moveItems($('a.sea'), -1000, 'easeInBack');
				
				hideFooterPop();

				/*$('a.land')
				.animate({
					opacity: 1
				})
				$('a.sea')
				.animate({
					opacity: .2
				});*/
			} else {
				$('#land')
				.removeClass('active')
				.data('clicked', 0);

				moveItems($('a.sea'));
				moveItems($('a.land'), -1000, 'easeInBack');

				showFooterPop('iala', 'http://ialathree.org');

				/*$('a.land')
				.animate({
					opacity: .2
				})
				$('a.sea')
				.animate({
					opacity: 1
				});*/
			}
		} else {
			$this.removeClass('active');
			
			hideFooterPop();

			moveItems($('a.land'));
			moveItems($('a.sea'));

			/*$('a.land, a.sea')
			.animate({
				opacity: 1
			});*/
		}
	});


	$(document.body).data('sim', 0);

	$('a.sim-projects')
	.click(function(e) {
		e.preventDefault();

		$('#land, #sea')
		.removeClass('active');

		$('a.land, a.sea')
		.animate({
			opacity: 1
		});

		var clicked = $(document.body).data('sim') == 1,
		toHide = !clicked ? $('#thumbs') : $('#thumbs-sim'),
		toShow = clicked ? $('#thumbs') : $('#thumbs-sim'),
		itemsUp = !clicked ? $('#thumbs .anim') : $('#thumbs-sim .anim'),
		itemsDown = clicked ? $('#thumbs .anim') : $('#thumbs-sim .anim');

		$(document.body).data('sim', $(document.body).data('sim') == 1 ? 0 : 1);

		var length = moveItems(itemsUp, -1000, 'easeInBack');

		toShow.show();

		var timer = setInterval(function() {
			if (itemIndex >= length) {
				timer = clearInterval(timer);
				toHide.hide();
				moveItems(itemsDown);
			}
		}, 100);
	});


	$(document.body).data('dev', 0);

	$('a.dev-projects')
	.click(function(e) {
		e.preventDefault();

		$('#land, #sea')
		.removeClass('active');

		$('a.land, a.sea')
		.animate({
			opacity: 1
		});

		var clicked = $(document.body).data('dev') == 1,
		toHide = !clicked ? $('#thumbs') : $('#thumbs-dev'),
		toShow = clicked ? $('#thumbs') : $('#thumbs-dev'),
		itemsUp = !clicked ? $('#thumbs .anim') : $('#thumbs-dev .anim'),
		itemsDown = clicked ? $('#thumbs .anim') : $('#thumbs-dev .anim');

		$(document.body).data('dev', $(document.body).data('dev') == 1 ? 0 : 1);

		var length = moveItems(itemsUp, -1000, 'easeInBack');

		toShow.show();

		var timer = setInterval(function() {
			if (itemIndex >= length) {
				timer = clearInterval(timer);
				toHide.hide();
				moveItems(itemsDown);
			}
		}, 100);
	});

	$('a.reference').click(function(e) {
		e.preventDefault();

		size = {
			x: $this.width().toInt(),
			y: $this.height().toInt()
		};

		pos = {
			x: $this.offset().left.toInt(),
			y: $this.offset().top.toInt()
		};

		openModal(e, 'ajax/CC_Reference.aGet', null, function($c) {
			$('<h2></h2>')
			.addClass('modal-title')
			.text('References')
			.prependTo($c);
		}, $(document).height() - fHeight - 20);
	});

	/*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($('#thumbs .anim'));

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

	bgLandLoop();
	bgSeaLoop();
});



