var PhilipsGgFlashId = "PhilipsGgFlash";

jQuery(document).ready(function() {
	
	var ScrollTop = function() {
	
		var scroll  = parent.document.body.scrollTop
		var timeout = 10;
		
		if(scroll == 0) { return; }
		
		setTimeout(function() {
			parent.window.scrollTo(0, scroll+Math.floor(((0 - scroll)/5)));
			ScrollTop();
		}, timeout)
	}
	
	var ProductsCarouselInitCallback = function(carousel) {
		jQuery('.products-carousel-next a').bind('click', function() {
	        carousel.next();
	        return false;
	    });
	
	    jQuery('.products-carousel-prev a').bind('click', function() {
	        carousel.prev();
	        return false;
	    });
	    
	    jQuery(".listPagination").each(function() {
		
			var links = jQuery("#ProductsCarousel ul > li").length;
		
			for(var x = 1; x <= links; x++) {
				jQuery(this).append('<li><a href="#" rel="' + x + '">' + x + '</a></li>');	
			}
			
			jQuery(this).find("a").click(function() {
				carousel.scroll(parseInt(jQuery(this).attr("rel")));	
				return false;
			});
		});

	    carousel.buttonNext.bind('click', function() {
	        carousel.startAuto(0);
	    });
	
	    carousel.buttonPrev.bind('click', function() {
	        carousel.startAuto(0);
	    });
	
	    carousel.clip.hover(function() {
	        carousel.stopAuto();
	    }, function() {
	        carousel.startAuto();
	    });
		
		if(top.location.hash != '') {
			var page = top.location.hash.replace("#/strona/", "").replace("/", "");
			setTimeout(function() {
				carousel.startAuto(0);
				carousel.scroll(page);
			}, 100);
		}
	}
	
	var ProductsOnBeforeAnimation = function(a,b,c,d) {
		jQuery(".listPagination a").removeClass("active");
		jQuery(".listPagination a[rel=" + c + "]").addClass("active");
	}
	
	jQuery('#ProductsCarousel').jcarousel({
		wrap: 'both',
		visible: 1,
		scroll: 1,
		auto: 4,
		initCallback: ProductsCarouselInitCallback,
		itemFirstInCallback: ProductsOnBeforeAnimation,
		easing: "easeOutExpo",
		animation: 2500
	});
	
	jQuery("#ProductsCarousel span.checkbox a").each(function() {
		jQuery(this).click(function() {
			if(jQuery(this).attr("rel") == "checked") {
				jQuery(this).attr("rel", "unchecked");
				UpdateScroll();
				jQuery(this).css("background-position", "0 -20px");
			}else {
				jQuery(this).attr("rel", "checked");
				UpdateScroll();
				jQuery(this).css("background-position", "0 0");
			}
			ScrollTop();
			
			return false;
		});		
	});
	
	var UpdateScroll = function() {
		var CurrentSum = 0;
		jQuery("#ProductsCarousel ul li > div").each(function() {
			var isChecked = jQuery(this).find("span.checkbox a").attr("rel");
			
			if(isChecked == "checked") {
				CurrentSum += parseInt(jQuery(this).find("input.price").val());
			}
		});
		
		document[PhilipsGgFlashId]["setPrice"](CurrentSum);
	}
	
	$('a[rel=external]').click( function() {
       window.open( $(this).attr('href') );
       return false;
   	});
});