var font_reset_em = 0.579;
//var em_step = 0.07;
var em_step = 0.071;
var font_current_step = 0;
var font_max_steps = 1;

function changeFontSize (new_size)
{
	var coeff = font_current_step + new_size;
	if (coeff <= font_max_steps && coeff >= 0)
	{
		font_current_step += new_size;
		font_current_em = font_reset_em + font_current_step * em_step;
		$("#container").css("fontSize", font_current_em + "em");
	}
}

function ToggleCategory (category_active)
{
	$("#categories_div").toggle();

	if ($("#categories_div").css('display') == 'block')
	{
		$("#menu2_2").removeClass("menu2_2");
		$("#menu2_2").addClass("menu2_2_active");
	}
	else
	{
		$("#menu2_2").removeClass("menu2_2_active");
		$("#menu2_2").addClass("menu2_2");
	}
}





var shown_index = -1;
var hide_all_timeout = null;

function CategoryOver (index)
{
	clearTimeout (hide_all_timeout);
	for (i = 0; i < 6; i++)
	{
		$('#categories_div_' + i).hide ();
	}
	$('#categories_div_' + index).show ();
	shown_index = index;
}

function CategoryOut (index)
{
	shown_index = -1;
	hide_all_timeout = window.setTimeout("CategoryHideAll()", 1500);
}

function CategoryHideAll ()
{
	if (shown_index == -1)
	{
		for (i = 0; i < 6; i++)
		{
			$('#categories_div_' + i).hide ();
		}
	}
}

function SubOver (index)
{
	clearTimeout (hide_all_timeout);
	shown_index = index;
}

function SubOut (index)
{
	shown_index = -1;
	hide_all_timeout = window.setTimeout("CategoryHideAll()", 1000);
}







function ShowZielGruppe (zielgruppe_parent_id, zielgruppe_child_id)
{
	//zielgruppe_parent_id = element.options[element.selectedIndex].value;
	$("#zielgruppe_child").load ("post_ajax.php?action=get_zielgruppe&parent_id=" + zielgruppe_parent_id + "&child_id=" + zielgruppe_child_id);
}

function ToggleAH ()
{
	$("#ah_container").toggle();


	if ($("#ah_container").css('display') == 'block')
	{
		document.getElementById("ah_img").src = "images/arrow_up.gif";
	}
	else
	{
		document.getElementById("ah_img").src = "images/arrow_down.gif";
	}
}

//clear the Content
function ClearField(sNameValue){
    document.getElementsByName(sNameValue)[0].value = "";
}

//write a standard text into the field
function GetStandardText(sNameValue,sTextValue){
    sValue = trim(document.getElementsByName(sNameValue)[0].value);

    if( sValue.length == 0 ){
        document.getElementsByName(sNameValue)[0].value = sTextValue;
    }else{
        if(sValue == sTextValue)
            ClearField(sNameValue);
    }
}

function TrimField(sNameValue){
    document.getElementsByName(sNameValue)[0].value=trim(document.getElementsByName(sNameValue)[0].value)
}

// removing whitespace
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function TogglePutCart (content_id)
{
	//debugger;
	var lt_cart_image = document.getElementById ("lt_cart_image_" + content_id);
	var lt_cart_hidden = document.getElementById ("lt_cart_hidden_" + content_id);
	var url_put = "post_ajax.php?action=cart_add&content_id=" + content_id;
	var url_remove = "post_ajax.php?action=cart_remove&content_id=" + content_id;
	var new_url;
	var new_src;
	var new_value;

	var hidden_value = parseInt (lt_cart_hidden.value);

    //document.getElementById('anzahl').value= "Sie haben "+hidden_value+" Produkte im Warenkorb";

	if (hidden_value == 1)
	{
		new_url = url_remove;
		new_src = "images/cart.gif";
		new_value = 0;
	}
	else
	{
		new_url = url_put;
		new_src = "images/cart_active.gif";
		new_value = 1;
	}
	lt_cart_image.src = new_src;
	lt_cart_hidden.value = new_value;

	$("#cartblock").load (new_url);
}

function TogglePutPlaylist (content_id, box_id, content_title)
{
	var lt_cart_image = document.getElementById ("lt_cart_image_" + content_id);
	var lt_cart_hidden = document.getElementById ("lt_cart_hidden_" + content_id);
	var url_put = "post_ajax.php?action=playlist_add&content_id=" + content_id + "&box_id=" + box_id + "&content_title=" + URLEncode(content_title);
	var url_remove = "post_ajax.php?action=playlist_remove&content_id=" + content_id;
	var new_url;
	var new_src;
	var new_value;

	if (lt_cart_hidden != null)
	{
		var hidden_value = parseInt (lt_cart_hidden.value);

		if (hidden_value == 1)
		{
			new_url = url_remove;
			new_src = "images/playlist.gif";
			new_value = 0;
		}
		else
		{
			new_url = url_put;
			new_src = "images/playlist_active.gif";
			new_value = 1;
		}
		lt_cart_image.src = new_src;
		lt_cart_hidden.value = new_value;
	}

	$("#playlist_block").load (new_url);
}

function RemoveFromPlayList (content_id)
{
	$("#playlist_block").load ("post_ajax.php?action=playlist_remove&content_id=" + content_id);
	setTimeout("location.reload(true)",1);
}

function MoveWindowToCenter (w, width, height)
{
	var Ycor = (screen.height-height) / 2;
	var Xcor = (screen.width-width) / 2;
	w.moveTo (Xcor, Ycor);
}

function OpenWindow (url, windowname, width, height)
{
	var w = window.open ('' + url + '','','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=yes,directories=no,location=no,width=' + width + ',height=' + height + '');
	MoveWindowToCenter (w, width, height);
}

function OpenFlashStreamWindow (url, windowname)
{
	var width = 590;
	var height = 380;
	OpenWindow (url, windowname, width, height)
}

function URLEncode (plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
}

function URLDecode (encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2)
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
	return plaintext;
}

function EraseOrGetField(fieldid,input_text,eraseorget)
{
    if(document.getElementById(fieldid).value == input_text && eraseorget == 'erase' )
        document.getElementById(fieldid).value = "";

    if(document.getElementById(fieldid).value == '' && eraseorget == 'get' )
        document.getElementById(fieldid).value = input_text;
}

function SubmitSearchForm (bundesland)
{
	radioObj = document.forms['search_form'].elements['shop_id'];
	radioLength = radioObj.length;
	var podcast = 0;
	for (var i = 0; i < radioLength; i++)
	{
		if (radioObj[i].value == '6' && radioObj[i].checked)
		{
			podcast = 1;
		}
	}

	var submit_link = document.getElementById ('submit_link');
	if (podcast == 1 || bundesland != '')
	{
		document.forms['search_form'].submit ();
	}
	else
	{
		show();
	}
}

