$().ready(function() {
  newsletterInputFocus();
  
  if (! $('body').hasClass("smallheader") && $('body#welcome').length > 0) {
    $('#wrapper').append('<div id="headimage"><ul></ul></div>');
    createLightswitch();
  }
   
  if ($.cookie("mydlightswitch") == "dark") {
    switchLightOff();
  } else {
    switchLightOn();
  }

  $("a.thickbox").fancybox({
    'zoomSpeedIn': 300,
    'zoomSpeedOut': 300,
    'overlayOpacity': 0.6,
    'overlayColor': '#333'
  });

  if ($('#shop .items').length > 0) {

    $('#shop .mode-default .items .item').each(function() {
      $(this).hoverIntent(function() {
        $(this).children(".tooltip").fadeIn('fast');
      }, function() {
        $(this).children(".tooltip").fadeOut('fast');
      });
      
      /*
      $(this).click(function() {
        window.location.href = $(this).find("a.details").attr("href");
      });
      */
      
    });

    container = $('#content .body .mode-default');
    itemsContainer = $('.items', container);
    itemsWidth = (itemsContainer.innerWidth() - container.outerWidth());
    itemWidth = itemsContainer.children('.item').outerWidth(true);
    products_count = itemsContainer.children('.item').length;

    productSlider = $('.slider', container).slider({
			min: 0,
			max: itemsWidth,
			step: itemWidth,
			slide: function (event, ui) {
				itemsContainer.animate({ left: (ui.value * -1) }, 100);
			},
			stop: function(event, ui) {
			  hideBothSlideButtons();
			  
			  if (ui.value <= itemsWidth && ui.value > 0) showLeftSlideButton();
			  if (ui.value < itemsWidth && products_count >= 3) showRightSlideButton();
			}
		});
  	
    showOrHideSlideButtons();

		$('a.browse.left').click(function() {
		  var l = parseInt(itemsContainer.css('left'));
		  var newl = l + itemWidth;
		  
		  if (newl > 0) newl = 0;

  		itemsContainer.animate({ left: newl }, 350, function() {
  		  productSlider.slider("value", ((l + itemWidth ) * -1));
  		  showOrHideSlideButtons(); 
  		});

		  return false;
		});
	
  	$('a.browse.right').click(function() {
  	  var l = parseInt(itemsContainer.css('left'));
    	  
  	  if (! l) {
  	    l = 0;
  	  }
  	  
  	  var newl = l + (itemWidth * -1);
  	  if ((newl * -1) > itemsWidth) newl = itemsWidth * -1;
  	  
  	  itemsContainer.animate({ left: newl }, 350, function() {
  	    productSlider.slider("value", (newl * -1));
    	  showOrHideSlideButtons();
  	  });
  
  	  return false;
  	});
  }
 	 
  if ($('#gallery').length > 0) {
    $('.thumbnails a').click(function() {
      $('#large img').attr("src", $(this).children("img").attr("src").replace(/([0-9]{2,4})x/, "/x" + $('#large img').attr("height") + "/"));
      $('#large').attr("href", $(this).attr("href"));
      
      $('#footercontent').html($(this).children("img").attr("alt"));
      $('#large').attr("title", $(this).children("img").attr("alt"));

      return false;
    });
    
    $('#content .scroller').scrollable({
      size: 2
    }).mousewheel();
  }
  
  if ($('#form_basket').length > 0) {
    if ($('fieldset.payment input[value=paypal]').attr("checked") == true) {
      if ($('#paypal_success').length == 0) {
        $('fieldset.non-paypal').hide();
      }
    } else {
      $('#paypal').hide();
    }
     
    $('fieldset.payment input[value=paypal]').click(function() {
      if ($('#paypal_success').length == 0) {
        $('fieldset.non-paypal').slideUp();
        $('#paypal').fadeIn();
      }
    });
     
    $('fieldset.payment input[value=bank]').click(function() {
      $('#paypal').slideUp();
      $('fieldset.non-paypal').slideDown();
    });
    
  }

});

function showOrHideSlideButtons() {
  if (productSlider.length > 0) {
    var pos = productSlider.slider("value");

    hideBothSlideButtons();
    
    if (pos >= itemsWidth) {
      showLeftSlideButton();
      
    } else {
      if (pos >= 0 && products_count >= 3) showRightSlideButton();
      if (pos > 0) showLeftSlideButton();
  
    }
    
  }
}

function hideBothSlideButtons() {
  $('a.browse.left').css("visibility", "hidden");
  $('a.browse.right').css("visibility", "hidden");
}

function showBothSlideButtons() {
  $('a.browse.left').css("visibility", "visible");
  $('a.browse.right').css("visibility", "visible");
}

function showRightSlideButton() {
  $('a.browse.right').css("visibility", "visible");
}

function showLeftSlideButton() {
  $('a.browse.left').css("visibility", "visible");
}

function newsletterInputFocus() {
  nlinitval = $('#newsletter_optin input[name=email]').attr("value");

  $('#newsletter_optin input[name=email]').focus(function() {
    if ($(this).attr("value") == nlinitval) {
      $(this).attr("value", '');
    }
  });

  $('#newsletter_optin input[name=email]').blur(function() {
    if ($(this).attr("value") == '') {
      $(this).attr("value", nlinitval);
    }
  });
}

function createLightswitch() {
  var mode = 'light';
  if ($('body').hasClass("light")) {
    mode = 'dark';
  }
  
  $('#head').append('<a href="?mode=' + mode + '"><img id="lightswitch" src="images/lightswitch_onoff.png" alt=""></a>')

  //$('#head').append('<img id="lightswitch" src="images/lightswitch_onoff.png" alt="">')
  //$('#lightswitch').click(toggleLight);
}

function toggleLight() {
  if ($('body').hasClass("light")) {
    switchLightOff();

  } else {
    switchLightOn();
    
  }
}

function switchLightOff() {
  $('body').removeClass("light").addClass("dark");
  //$('#lightswitch').attr("src", "images/lightswitch_on.png");
  $('#lightswitch').attr("src", "images/lightswitch_onoff.png");

  $.cookie("mydlightswitch", "dark", { expires: 30, path: cookiepath });

  if ($('body.smallheader').length == 0) {
    $('#headimage ul').find("li").remove();
    $('#headimage ul').append('<li><img src="images/head/dark/001.jpg"></li>');
    $('#headimage ul').append('<li><img src="images/head/dark/002.jpg"></li>');
    
    $('#headimage ul').innerfade({
  	  animationtype: 'fade',
  	  speed: 1500,
  	  timeout: 6000,
  	  type: 'random'
	  });
  }
  
}

function switchLightOn() {
  $('body').removeClass("dark").addClass("light");
  //$('#lightswitch').attr("src", "images/lightswitch_off.png");
  $('#lightswitch').attr("src", "images/lightswitch_offon.png");
  
  $.cookie("mydlightswitch", "light", { expires: 30, path: cookiepath });
    
  if ($('body.smallheader').length == 0) {
    $('#headimage ul').append('<li><img src="images/head/light/001.jpg"></li>');
    $('#headimage ul').append('<li><img src="images/head/light/002.jpg"></li>');
    $('#headimage ul').append('<li><img src="images/head/light/003.jpg"></li>');
    $('#headimage ul').append('<li><img src="images/head/light/004.jpg"></li>');

  	$('#headimage ul').innerfade({
  	  animationtype: 'fade',
  	  speed: 1500,
  	  timeout: 6000,
  	  type: 'random'
	  });
  	
  }
  
}

