$(document).ready(function() {	    
	/*
	 * png fix
	 */
		$('#headerLogo, form input.submit, div.split div.splitbox img, img.arrowOrange').ifixpng();
	    $('div.buy a img').ifixpng();
		
	/*
	 * add to fav button
	 */
		$('#addToFavorite').jFav();

	/*
	 * a to z categoty functions
	 */
		$('#showAllBoxes').click( function() {
			$('.tabheadline').each( function(){
				$(this).parent().attr("id","shown");
				$(this).parent().find('.categories').slideDown("slow");
			});
		});
	    $('.tabheadline').click( function() {
	    	hideAllCategories();
			$(this).parent().attr("id","shown");
			$(this).parent().find('.categories').slideDown("slow");
	    });
	    function hideAllCategories() {
	    	$('.tab').each(function(){
				$(this).attr("id","");
	    		$(this).find('.categories').slideUp("fast");
	    	});
	    }
	    //$(".tabheadline:first").trigger('click');
	    $("#showAllBoxes").trigger('click');

	/*
	 * slider
	 */
	    $('#sliderCarousel').jcarousel({
	    	scroll: 4,
	    	buttonNextHTML: '<div></div>',
	    	buttonNextEvent: 'click',
	    	buttonPrevHTML: '<div></div>',
	    	buttonPrevEvent: 'click'
	    });
		$('img.reflect').reflect({
			height: 0.2, 
			opacity: 0.6
		});    	
});

function redirectToURL(url){
	if(url!=""){
		window.location = url;
	}
}

/*
 * BASKET
 * FUNCTIONS
 */

function updateShoppingBasket() {
    $(document).ready(function() {
    	$(".box_small_content_basket #displayArea").hide();
    	$(".box_small_content_basket #refreshArea").show();
    	$.get("dispatch.do?def=.hs24.menu.basket", function(data){
    		$("#basketcontainer").html(data);
		});
    });
}

function addToShoppingBasketInternal(buyCode) {
	var shoppingBasket = readCookie("shoppingBasket");
	if (shoppingBasket && shoppingBasket.indexOf(buyCode) != -1) {
		return;
	}
	if (shoppingBasket) {
		shoppingBasket = shoppingBasket + "," + buyCode;
	} else {
		shoppingBasket = buyCode;
	}
	
	var now = new Date();
	var minutes = now.getMinutes();
	
	//also needs to be set in ShoppingBasketUtils
	minutes += 7200; // ( 24 Hours * 60 Minutes ) * 5 Days = 7200 Minutes
	now.setMinutes(minutes);
	
	writeCookie("shoppingBasket", shoppingBasket, now, "/", null, null);
}

function addToShoppingBasket(buyCode) {
	if(canCookie()){
		addToShoppingBasketInternal(buyCode);
	    updateShoppingBasket();	
	}
}

function addTracksToShoppingBasket() {
    if(canCookie()){
	    var i;
	    var addedNumber = 0;
	    for (i=0;i<document.tracklist.length;i++) {
	      if (document.tracklist.elements[i].checked) {
	        addToShoppingBasketInternal(document.tracklist.elements[i].value);
	        addedNumber+=1;
		  }
	    }
	    if(addedNumber>0){
	    	updateShoppingBasket();
	    }else{
	    	alert("Bitte wählen Sie zunächst eine Menge von Titeln für den Warenkorb aus.");
	    }
    }
}

/*
 * COOKIE
 * FUNCTIONS
 */

function canCookie(){
	writeCookie("test", "hoerspiel24", null, "/", null, null);
	if (readCookie("test")=="hoerspiel24")
	{
		removeCookie("test","/",null)
		cookie_set = true;
		return true;
	}
}

function readCookie(name) {
   var theCookie = document.cookie;

   // Anfangsposition des Name=Wert-Paars suchen
   var posName = theCookie.indexOf("; " + name + "=");
   if (posName == -1) {
      // vielleicht war's der erste Name in der Liste?
      if (theCookie.indexOf(name + "=") == 0) posName = 0;
      // nein? dann abbrechen mit R?ckgabewert null
      else return null;
   }

   // Anfangs- und Endposition des Kr?melwerts suchen
   var wertAnfang = theCookie.indexOf("=", posName)+1;
   var wertEnde = theCookie.indexOf(";", posName+1);
   if (wertEnde == -1) wertEnde = theCookie.length;

   // Kr?melwert auslesen und zur?ckgeben
   var wert = theCookie.substring(wertAnfang, wertEnde);
   return unescape(wert);
}

function writeCookie(name,wert,verfall,path,dom,secure) {
   neuerKeks = name + "=" + escape(wert);
   if (verfall)
      neuerKeks += "; expires=" + fixedGMTString(verfall);
   if (path) neuerKeks += "; path=" + path;
   if (dom) neuerKeks += "; domain=" + dom;
   if (secure) neuerKeks += "; secure";
   document.cookie = neuerKeks;
}

function removeCookie( name, path, dom ) {
	document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( dom ) ? ";domain=" + dom : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function fixedGMTString(datum){
	var damals=new Date(1970,0,1,12);
	if (damals.toGMTString().indexOf("02")>0) {
	   datum.setTime(datum.getTime()-1000*60*60*24);
	}
	return datum.toGMTString();
}

/*
 * TRIM
 * FUNCTIONS
 */

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}