$(document).ready(function() 
{	
	var RUN             = false;	
	var speed           = "slow";
	var delay           = 350;	
	var showProducts    = 3;
	var productcount    = $('#productcount').val();	
	var leftendval      = 1;
	var rightendval     = productcount;	
	var currentIndex    = 2;	
	var outerleftIndex  = currentIndex - 1; 
	var outerrightIndex = currentIndex + 1; 
	var navPadding      = new Array(15, 25, 35, 45, 55);
	
	var productboxSize                = new Array();
	productboxSize['small']           = new Array();
	productboxSize['small']['width']  = 100;
	productboxSize['small']['height'] = 120;
	productboxSize['big']             = new Array();
	productboxSize['big']['width']    = 160;
	productboxSize['big']['height']   = 180;
	
	$("body").attr("id","js");
	
	$('#scrollproduct_'+outerrightIndex).height(productboxSize['small']['height']);
	$('#scrollproduct_'+outerrightIndex).width(productboxSize['small']['width']);	
	$('#scrollproduct_'+outerleftIndex).height(productboxSize['small']['height']);
	$('#scrollproduct_'+outerleftIndex).width(productboxSize['small']['width']);
	
	$('#scrollTable td').css('width',  $('#slider_box').width() / showProducts );
	
	for (i = outerrightIndex + 1; i <= productcount; i++)
	{
		$('#scrollproduct_'+i).height(productboxSize['small']['height']);
		$('#scrollproduct_'+i).width(productboxSize['small']['width']);	
	}
	
	for (i = currentIndex + 2; i <= productcount; i++)
	{
		$('#scrollproduct_' + i).slideToggle('fast');	
	}

	$('#sliderleftbutton').bind({		
		click: function() {
			if ( ( currentIndex - 1 ) >= leftendval )
			{
				if ( !RUN )
				{
					RUN              = true;
					currentIndex    -= 1;
					outerleftIndex  -= 1;
					outerrightIndex -= 1;
					
					$('#scrollproduct_' + ( outerrightIndex + 1 ) ).slideToggle('fast');						
					$('#scrollproduct_' + outerleftIndex).delay(delay).slideToggle('fast');
					
					$('#slide_product_text_' + outerrightIndex).fadeOut('fast');
					
					$('#scrollproduct_' + outerrightIndex).animate({
						width: productboxSize['small']['width'],
						height: productboxSize['small']['height']						
					}, speed);	
								
					$('#scrollproduct_' + currentIndex).animate({
						width: productboxSize['big']['width'],
						height: productboxSize['big']['height']						
					}, speed, function() { 
						$('#slide_product_text_' + currentIndex).fadeIn('fast'); 
					});	
		
					$('#slider_box').animate({"left": "+=" + $('#scrollbox_' + leftendval).width() + "px"}, speed, function() 
					{ 
						RUN = false;												
					});
				}		
			}					
		}		
	});

	$('#sliderrightbutton').bind({ 		
		click: function() {			
			if ( ( currentIndex + 1 ) <= rightendval ) 
			{
				if ( !RUN ) 
				{
					RUN              = true;				
					currentIndex    += 1;
					outerleftIndex  += 1;		
					outerrightIndex += 1;
					
					$('#scrollproduct_' + ( outerleftIndex - 1 ) ).slideToggle('fast');					
					$('#scrollproduct_' + outerrightIndex).delay(delay).slideToggle('fast');
					
					$('#slide_product_text_' + outerleftIndex).fadeOut('fast');
					
					$('#scrollproduct_' + outerleftIndex).animate({
						width: productboxSize['small']['width'],
						height: productboxSize['small']['height']						
					}, speed);
														
					$('#scrollproduct_' + currentIndex).animate({
						width: productboxSize['big']['width'],
						height: productboxSize['big']['height']						
					}, speed, function() {						
						$('#slide_product_text_' + currentIndex).fadeIn('fast'); 						
					});
						
					$('#slider_box').animate({ left: "-=" + $('#scrollbox_' + leftendval).width() + "px"}, speed, function() 
					{ 
						RUN = false;	
					});				
				}		
			}	
		}
	});
	
	$('#sliderrightbutton').hover(
		function() {
			$('#sliderrightbutton_effect').stop().fadeIn('fast');
		},
		function() {
			$('#sliderrightbutton_effect').fadeOut('slow');
		}
	);

	$('#sliderleftbutton').hover(
		function() {
			$('#sliderleftbutton_effect').stop().fadeIn('fast');
		},
		function() {
			$('#sliderleftbutton_effect').fadeOut('slow');
		}
	);
	
	$("div#jEventsArticleBox").each(function(e) {
		$(this).hover(
		function() {
			$(this).stop().animate({borderBottomColor: "#FF9600", borderLeftColor: "#FF9600", borderRightColor: "#FF9600", borderTopColor: "#FF9600"}, "slow");
		},
		function() {
			$(this).stop().animate({borderBottomColor: "#646464", borderLeftColor: "#646464", borderRightColor: "#646464", borderTopColor: "#646464"}, "slow");
		});
	});	
	
	$('#tabs').tabs({ fx: [{height:'toggle', opacity: 'toggle', duration:'slow'},
                           {height:'toggle', opacity: 'toggle', duration:'slow'}] }).find(".ui-tabs-nav").sortable({axis:'x'});;
		
   	$('#support-tab').unbind();   

   	$('#support-tab').bind({
   		click: function() {
   			window.open('http://www.hori-zont.de/livezilla/chat.php','','width=600,height=600,left=0,top=0,resizable=yes,menubar=no,location=yes,status=yes,scrollbars=yes');
   		}
   	});	   
                          
	/*
	$('#support-tab').bind({ 		
		click: function() {	
			alert('click');
			jQuery.ajax({
	            type: 'post',
	            url: 'http://www.hori-zont.de/livezilla/chat.php?code=U0VSVkVSUEFHRQ__',
	            dataType: 'html',
	            success: function (html) {  
	            	alert(html);
	              	$('#tabs-7').html(html);
	            }
	        });
	        
		} 
	});        
	*/
	   
	$('a#nav_element').each( function( index ) {		
		$(this).hover(
			function() {
				if (!$(this).is ('.active'))
				{
					$(this).stop().animate({ color: "#FFFFFF", backgroundColor: "#FF9600", paddingLeft: ( navPadding[ parseInt( $(this).attr('rel') ) ] + 10 ) }, "fast");
				}
				else
				{
					$(this).stop().animate({ paddingLeft: ( navPadding[ parseInt( $(this).attr('rel') ) ] + 10 ) }, "fast");	
				}
			},
			function() {				
				if (!$(this).is ('.active'))	
				{			
					$(this).stop().animate({ color: "#000000", backgroundColor: "#E6E6E6", paddingLeft: navPadding[ parseInt( $(this).attr('rel') ) ] }, "slow");
				}
				else				
				{
					$(this).stop().animate({ paddingLeft: navPadding[ parseInt( $(this).attr('rel') ) ] }, "slow");		
				}	
			}
		);
	});
	
	/*
	$('a#tophomelink_mouse').each(function() {
		$(this).hover(
			function()  {
				$('#tophomelink_effect_' + $(this).attr('rel') ).fadeIn('fast');
			},
			function() {
				$('#tophomelink_effect_' + $(this).attr('rel') ).fadeOut('slow');
			}
		);	
	});
	*/
		
	$.fx.speeds._default = 500;
	
	$(function() {
		$("#add_to_cart").dialog({        
	        autoOpen: false,
	        width: 350,
	        modal: true,        
	        show: "explode",
	        hide: "explode",        
	        resizable: false,
	        buttons: {
	            'Weiter einkaufen': function() {                
	                $(this).dialog('close');    
	            },
	            'Zur Kasse': function() {
					$(this).dialog('close');
	            	document.location.href = 'http://www.hori-zont.de/checkout/cart.html';      	
	            }
	        }
	    });
	});

	$('#variant_change').change(function() {		
		ChangeProductDetails($(this).val());		
	});
	
	function CheckoutStepsAnimation() 
	{
		$('.content_checkout_steps ol li.active').animate({ backgroundColor: "#FF9600"}, 1500, function() {
			$(this).animate({ backgroundColor: "#FFF"}, 1500, function() { 
				CheckoutStepsAnimation();	
			});
		});		
	}
		
	CheckoutStepsAnimation();
	
	
	$('#renewProduct').css('display', 'none');
	
	$('#CustomerReview').bind({ 		
		click: function() {	
			$(this).attr('disabled', true);
			$.ajax({
	            type: 'POST',
	            url: 'http://www.hori-zont.de/product/review/send',
	            dataType: 'html',
	            data: $('#tabs-8 input, #tabs-8 textarea'),
	            success: function (html) {  
	            	
	            	$('#CustomerReviewButton').html('Produktempfehlung Versendet!');
	            	
	            }
	        });	   
		} 
	});	
	
});

function ChangeProductDetails(id)
{
	jQuery.ajax({
        type: 'post',
        url: '/ajax/product/image/' + id,
        dataType: 'html',
        success: function (html) {     		
      		var args = html.split(";");              		
      		$('#srpContainer').html(args[2]);
      		$('#savingContainer').html(args[3]);
      		$('#priceContainer').html(args[1]);
      		$('#product_image_container').html(args[0]);               		              		           		
    		$('.image').attr('id', 'image'+$('#imageid').val());   
    		jQuery.ajax({
    			type: 'post',
			    url: '/ajax/product/set/' + args[4],
			    dataType: 'html',
        		success: function (html) {        			
        			$('#setProductsContainer').html(html);	        			
        		}
    		});  		
        }
    });	
}

function CreateFlyingCart(id)
{
	jQuery('input#add_to_cart' + id).click(function () {	
    	jQuery.ajax({
            type: 'post',
            url: '/ajax/checkout/cart/add/product',
            dataType: 'html',
            data: jQuery('#prod'+id+' :input'),
            success: function (html) {       	
                jQuery('#module_cart').html(html);
            },
            complete: function () {          	
                var image = $('#image'+id).offset();
                var cart  = $('#module_cart').offset();
                
                jQuery('body').after('<img src="' + $('#image'+id).attr('src') + '" id="temp'+id+'" width="200" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px; z-index:99;" />');
               
                params = {
                    top : cart.top + 'px',
                    left : cart.left + 'px',
                    opacity : .2, 
                    width: 50,                   
                    heigth : 100
                };
                
                specialOptions = {
                	opacity: 'easeInExpo',
                	width:   'easeInExpo', 
                	height:  'easeInExpo'	                	
                };
                
                jQuery('#temp'+id).animate(
                	params, { 
                		duration: 1000, 
                		specialEasing: specialOptions, 
                		complete: function () {          	
		                	jQuery('#temp'+id).remove();
		                	$('#CartEventBoxText').html($('#image'+id).attr('title') + ' zum Warenkorb Hinzugef&uuml;gt!');
		                	$('#add_to_cart').dialog('open'); 	              	
                		}
                	}
                );
            }
        });
        return false;
    });
}
