jQuery.noConflict();

jQuery(document).ready(function() {
	(function($) {
	    $.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
			    $(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
			    callback();
		    });
	    };
	    $.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
			    $(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
			    callback();
		    });
	    };
	})(jQuery);


	var bool = 1;
	var item = 0;
	var items = jQuery("#slideshow li").size();
	var timer = setInterval(rotate_right, 5000);
	items = items-1;
	var prev = items;

	var save;

	jQuery("#slideshow li").hide();
	
	for (var i = items; i >= 0; i--)
	    {
		var value = jQuery("#slide-squares").html();
		var add = "<li></li>";
		jQuery("#slide-squares").html(add+value);
	    }
	
	function rotate_right() {
	    bool = 0;
	    jQuery("#slide-squares li").eq(items-item).addClass("slide-squares-current");
	    jQuery("#slide-squares li").eq(items-prev).removeClass("slide-squares-current");
	    jQuery("#slideshow li").eq(prev).css({zIndex:99}).customFadeOut(500);
	    jQuery("#slideshow li").eq(item).css({zIndex:90}).customFadeIn(500, function() {
		    jQuery("#slideshow li").eq(item).css('filter', '');
		    bool = 1;
		});
	    save = item;
	    item = (item == items) ? 0 : item+1;
	    prev = (prev == items) ? 0 : prev+1;
	}
	
	function rotate_left() {
	    bool = 0;
	    prev = (prev == 0) ? items : prev-1;
	    item = (item == 0) ? items : item-1;
	    jQuery("#slide-squares li").eq(items-item).removeClass("slide-squares-current");
	    jQuery("#slide-squares li").eq(items-prev).addClass("slide-squares-current");
	    jQuery("#slideshow li").eq(prev).stop().fadeIn(500);
	    jQuery("#slideshow li").eq(item).stop().fadeOut(500, function() {
		    bool = 1;
		    });
	}

	jQuery("#prev").click(function() {
		if (bool) {
		    clearInterval(timer);
		    rotate_left();
		    timer = setInterval(rotate_right, 5000);
		}
	    });

	jQuery("#next").click(function() {
		if (bool) {
		    clearInterval(timer);
		    rotate_right();
		    timer = setInterval(rotate_right, 5000);
		}
	    });
	
	jQuery("#slideshow li").mouseover(function() {
		clearInterval(timer);
	    });

	jQuery("#slideshow li").mouseout(function() {
		timer = setInterval(rotate_right, 5000);
	    });
	
	jQuery("#slide-squares li").mouseover(function() {
		if (bool) {
		    bool = 0;
		    clearInterval(timer);
		    item = items - jQuery("#slide-squares li").index(this);
		    jQuery("#slide-squares li").eq(items-save).removeClass("slide-squares-current");
		    jQuery(this).addClass("slide-squares-current");
		    jQuery("#slideshow li").eq(save).stop().fadeOut(500);
		    jQuery("#slideshow li").eq(item).stop().fadeIn(500, function() {
			    bool = 1;
			});
		    save = item;
		    item = (item == items) ? 0 : item+1;
		    prev = (item == 0) ? items : item-1;
		    timer = setInterval(rotate_right, 5000);
		}
	    });

	rotate_right();

    });

