$(document).ready(function() {
	$('HTML').addClass('JS'); 
	$('#cc p:last').addClass('last');
	$('#pnav').height($('#pbar').height() + $('#cc').height() - 5);

	function slide_bottom(link) {

		var bot = link.replace('page', 'ajax/page/get_bottom');
		var bot = bot.replace('category', 'ajax/page/get_bottom');

		$('#cb2 .cs').fadeOut(500);
		$('#cb2 .csw').addClass('loading');
		$('#cb2 .backb > a').detach();
		$('#cb2 .backb').removeClass('active');

		$('#cb2 .cs').load(bot,
			function() {

				$('#bproducts li').each(
					function(i, v){
						var self = this;
						var src = $('img', this).attr('src');
						$('img', this).replaceWith('<div class="loader loading"></div>');
						var img = new Image();

						$(img)
							.load(
								function () {
									$(this).hide();

									$('.loader', self)
										.removeClass('loading')
										.append(this);

									$(this).fadeIn();
								}
							)
							.attr('src', src);
					}
				);
				$('#cb2 .cs').fadeIn(500);

				$('#cb2 .csw').removeClass('loading')

				$('#bproducts a.pimg').lightBox({
					imageLoading:			'layouts/front/themes/default/imgs/lb/lightbox-ico-loading.gif',
					imageBtnPrev:			'layouts/front/themes/default/imgs/lb/lightbox-btn-prev.gif',
					imageBtnNext:			'layouts/front/themes/default/imgs/lb/lightbox-btn-next.gif',
					imageBtnClose:			'layouts/front/themes/default/imgs/lb/lightbox-btn-close.gif',
					imageBlank:				'layouts/front/themes/default/imgs/lb/lightbox-blank.gif',
				});

				var lipar = $('#products > li a[href$="'+ link + '"]').parentsUntil('#products').filter('li');
				if ($(lipar).length > 1) {
					$('#cb2 .backb').addClass('active');
					$('#cb2 .backb').append('<a href="#">back</a>');

					$('#cb2 .backb > a').attr('href', $('a:first', lipar.eq(lipar.length - 1)).attr('href'));
					$('#cb2 .backb > a').live('click',
						function(e) {
							var url = $(this).attr('href').replace('/gemeurope_kohana/trunk/www/', '');
							url = url.replace(/^.*#/, '');
							$.history.load(url);
							return false;
						}
					);
				}
			}
		);
	}

	$.post(baseurl + "ajax/gallery", function(data){
		if(data.length >0) {
			myObject = undefined;
			try {
				myObject = eval(data);
			}
			catch(err) {
			}

			if (myObject !== undefined) {
				$('#img1').replaceWith('<div id="img1"></div>');
				$('#img2').replaceWith('<div id="img2"></div>');
				$('#img3').replaceWith('<div id="img3" class="last"></div>');
				var pcnt = myObject.length;
				$('#img2').crossSlide({sleep:8,fade:2}, myObject.slice(Math.floor(pcnt/3), 2*Math.floor(pcnt/3)-1));
				$('#img1').crossSlide({sleep:4,fade:1}, myObject.slice(0, Math.floor(pcnt/3)-1));
				$('#img3').crossSlide({sleep:4,fade:1}, myObject.slice(2*Math.floor(pcnt/3), 3*Math.floor(pcnt/3)-1));
			}
		}
		else {
			//$('#imgcontainer').hide();
		}
	});

	function load(num) {
		var href = num;
		num = num.replace('category', 'ajax/category/get_description');
		num = num.replace('page', 'ajax/page/get_content');

		var lapsar = [];
		var apsar = [];

		if ($.history.lastappState) {
			lapsar = $.history.lastappState.replace(/^\//, '').split('/');
		}

		if ($.history.appState) {
			apsar = $.history.appState.replace(/^\//, '').split('/');
		}

		if ((lapsar[1] != apsar[1] && lapsar[2] != apsar[2]) || (lapsar[1] == apsar[1] && lapsar[2] != apsar[2])) {
			var len = $('#cb1 .cs').length;
			$('#cb1 .cs:lt(' + (len) + ')').addClass('off');

			$('#cb1 .cs:last').after('<div class="cs" style="position: absolute; float: none; left: 717px; top: -' + $('#cc .cb1 .cs:first').height() + '"></div>');
			$('#cb1 .cs:last').css({'opacity' : 0});

			var numar = num.replace(/^\//, '').split('/');
			var num = '';
			for (var kk=0; kk<=4; kk++) {
				num += numar.shift() + '/';
			}

			$('#cb1 .cs:last').load(baseurl + num,
				function(){
					$('#cb1 p:last').addClass('last');
					var fh = $('#cb1 .cs:first').height();
					var lh = $('#cb1 .cs:last').height();

					var src = $('img.pcm', this).attr('src');
					$('img.pcm', this).replaceWith('<div class="loader loading"></div>');

					var img = new Image();
					$(img)
						.load(
							function () {
								$(this).hide();

								$('.loader', '#cc .cb1 .cs:last')
									.removeClass('loading')
									.append($('<a href="' + src.replace('/p/cm', '/p/l') + '"></a>').append(this));

								$(this).slideDown();

								$(this).parent('a').lightBox({
									imageLoading:			'layouts/front/themes/default/imgs/lb/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
									imageBtnPrev:			'layouts/front/themes/default/imgs/lb/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
									imageBtnNext:			'layouts/front/themes/default/imgs/lb/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
									imageBtnClose:			'layouts/front/themes/default/imgs/lb/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
									imageBlank:				'layouts/front/themes/default/imgs/lb/lightbox-blank.gif',			// (string) Path and the name of a blank image (one pixel)
								});
							}
						)
						.error(
							function () {
							}
						)
						.attr('src', src);

					var link = $('a[href$="' + href + '"]', '#products, #mnav');
					var link_li = link.closest('li');
					var link_si = link_li.siblings('li');

					if (link_si.length > 1) {
						var next = link_li.next('li');
						var prev = link_li.prev('li');

						if (next.length > 0) {
							next = next.children('a');
							if (next.length <= 0) {
								next = $('.pcontainer .vcenter a', link_li.next('li'));
							}
						}
						else {
							next = link_li.siblings('li:first').children('a');
							if (next.length <= 0) {
								next = $('.pcontainer .vcenter a', link_li.siblings('li:first'));
							}
						}

						if (prev.length > 0) {
							prev = prev.children('a');
							if (prev.length <= 0) {
								prev = $('.pcontainer .vcenter a', link_li.prev('li'));
							}
						}
						else {
							prev = link_li.siblings('li:last').children('a');
							if (prev.length <= 0) {
								prev = $('.pcontainer .vcenter a', link_li.siblings('li:last'));
							}
						}

						$('h2', '#cb1 .ccp:last').append('<a class="ctnext ctnr" href="' + next.attr('href') + '">' + next.text() + '&raquo;</a><a class="ctnext ctnl" href="' + prev.attr('href') + '">&laquo;' + prev.text() + '</a>');
					}

					if (fh < lh) {
						$('#cb1 .cs:first')
							.animate({height : lh}, 500);
					}
					else {
						$('#cb1 .cs:last').height(fh);
					}

					$('#cb1 .cs:last').css({'float' : 'left', 'position' : 'static', 'top' : 0, 'left' : '0'});

					if (fh > lh) {
						$('#cb1 .cs.off:first').stop(false, true).animate({'opacity' : 0, 'margin-left': '-=717', 'height' : lh}, 2000, 'jswing');

						$('#cb1 .cs:last')
							.animate({'opacity' : 1, 'height' : lh}, 2000, 'swing',
								function () {
									$('#cb2 .csw').removeClass('loading');
									$('#cb1 .cs.off:first').detach();
								}
							);
						$('#pnav').animate({height : ($('#pbar').height() + 296 + lh - 16)}, 1000);
					}
					else {
						$('#cb1 .cs:last').animate({'opacity' : 1}, 2000);
						$('#pnav').height($('#pbar').height() + 296 + lh - 16);
						$('#cb1 .cs.off:first')
							.stop(false, true)
							.animate({'margin-left': '-=717'}, 2000, 'swing',
								function() {
									//$('#cb2 .csw').removeClass('loading');
									$('#cb1 .cs.off:first').detach();
								}
							);
					}

				}
			);
		}

		if (apsar[1] != lapsar[1] || apsar[1] != 'page') {
			slide_bottom(href);
		}
	}

	$.history.init(function(url) {
		if (url != '') {
			load(url);
		}
	});

	$('#products a, #mnav a, a.ctnext, #bproducts a.ptext').live('click', function(e) {
			var url = $(this).attr('href').replace(baseurl, '');
			url = url.replace(/^.*#/, '');
			$.history.load(url);
			return false;
	});

	$('#bproducts a.pimg').lightBox({
		imageLoading:			'layouts/front/themes/default/imgs/lb/lightbox-ico-loading.gif',
		imageBtnPrev:			'layouts/front/themes/default/imgs/lb/lightbox-btn-prev.gif',
		imageBtnNext:			'layouts/front/themes/default/imgs/lb/lightbox-btn-next.gif',
		imageBtnClose:			'layouts/front/themes/default/imgs/lb/lightbox-btn-close.gif',
		imageBlank:				'layouts/front/themes/default/imgs/lb/lightbox-blank.gif',
	});

});
