$(document).ready(function() {

	// Activate carousel function
	$("#carousel1").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 4000);

	// Activate carousel function
	$("#carousel2").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 4000);

	// Activate accordion function
	$( ".accordion" ).accordion({active:false,autoHeight:false,collapsible:true});

	// Activate Table of Contents
	if($("#toc").length!==0){
	$.getScript('/inc/js/TableOfContent.js', function() {
	    $('#toc').TableOfContentsBehavior('#mainContent', 'ul');
	});
	}

	// Activate tabs function
	$("#box-tabs").tabs();

	// Activate Fancybox function
	if($("a.fancybox").length!==0){
        $('head').append('<link href="/inc/css/jquery.fancybox-1.3.4.css" rel="stylesheet" type="text/css" media="all" />');

	$.getScript('/inc/js/jquery.fancybox-1.3.4.pack.js', function() {
	$("a.fancybox").each(function(i){
		var href = $(this).attr("data-overlay");
			$(this).fancybox({
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'href'		: href,
			'titleShow'	: false,
			});
	});
	});
	}

 // Change product image on mouseover
   $(".thumb").click(function(e){
    // stop normal link click
     e.preventDefault();
	// get image url
	var url = $(this).attr("href");
	// change image url to one of thumb
       $("#image").attr(
         'src', "/includes/thumb_big.php?picture=" + url
       );
       $("#image-href").attr(
         'href', url
       );
   });


	// Activate Cloud Zoom
	if($(".cloud-zoom").length!==0){
	$.getScript('/inc/js/cloud-zoom.1.0.2.min.js');
	}
	// Zoom product image on mouseover
	$(".cloud-zoom").click(function(e){
	// stop normal link click
	e.preventDefault();
	// Load Cloud Zoom
	$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
	});

   // Change product on overview page function
   $(".product-color").click(function(e){
     // stop normal link click
     e.preventDefault();
     // get product id
	var product_id = $(this).attr("name");
     // send request
     $.post("/includes/server_product.php", {product: product_id}, function(xml) {
       // format and output result
	var id = $("parent_id", xml).text(); // color independent ID
       $("#image-" + id).attr(
         'src', "/includes/thumb_medium.php?picture=" + $("image", xml).text()
       );
       $("#zoom-" + id).attr(
         'href', "/product/" + $("product_id", xml).text() + "/"
       );
       $("#details-" + id).attr(
         'href', "/product/" + $("product_id", xml).text() + "/"
       );
       $("#title-" + id).html(
         $("title", xml).text()
       );
       $("#description-" + id).html(
         $("description", xml).text()
       );
       $("#price-" + id).html(
         $("price", xml).text()
       );

       $("#product-" + id).attr(
         'value', $("product_id", xml).text()
       );
	var size = $("size", xml).text();
	var exploded = size.split(';');
    exploded = jQuery.map(exploded, function(n, i){
      return ("<option value=" + n + ">" + n +"<\/option>");
    });
    $("#size-" + id).html(exploded.join(", "));
     });
   });


   	// Change Rollover Image
	if($(".rollover").length!==0){
	$.getScript('/inc/js/tooltip.js');
	}
	$(".rollover").hover(
		function () {
			// get product id
			var product_id = $(this).parent().attr("id");
			var product_title = $(this).attr("alt");
			$.post("/includes/server_product.php", {product: product_id}, function(xml) {
			    $("#rollover-image img").attr(
				'src', "/includes/thumb_medium.php?picture=" + $("image", xml).text()
				);
				$("#rollover-image span").html(
				product_title
				);
			});
		}
	);
	// Load and activate Rollover Image
	if($(".rollover").length!==0){
	$.getScript('/inc/js/tooltip.js', function() {
	    $(".rollover").ezpz_tooltip({contentId:"rollover-image"});
	});
	}
	
	// Show and Hide Coupon Code on Shoppingbag page
	$("#couponcode-show").click(function(e){
     e.preventDefault();
      $("#couponcode-show").hide();
      $("#couponcode").show(1000);
    });



// Activate Tooltip
ShowTooltip = function(e)
{
	var text = $(this).next('.show-tooltip-text');
	if (text.attr('class') != 'show-tooltip-text')
		return false;
	text.fadeIn()
	return false;
}
HideTooltip = function(e)
{
	var text = $(this).next('.show-tooltip-text');
	if (text.attr('class') != 'show-tooltip-text')
		return false;

	text.fadeOut();
}
SetupTooltips = function()
{
	$('.show-tooltip')
		.each(function(){
			$(this)
				.after($('<span/>')
					.attr('class', 'show-tooltip-text')
					.html($(this).attr('title')))
				.attr('title', '');
		})
		.hover(ShowTooltip, HideTooltip);
}
	SetupTooltips();



});

