
function cart_init()
{
	binder();
}

function general_init()
{
	autoscroll = true;
	//highlight_valentin();
	/*
	$("body").click( function(e)//$("#graymeout").bind("click", function(e)
		{
			//alert(e.target+"you clicked me!");
			if(!$(e.target).is('layer-holder-big') && !$(e.target).is('layer-holder-small') && !$(e.target).is('layer-holder-huge'))
			{
				$("#graymeout").fadeOut("fast");
				$('.layer-box-small').fadeOut("fast");
				$('.layer-box-big').fadeOut("fast");
				$('.layer-box-huge').fadeOut("fast");
			}
		}
	);*/
	set_pp_category_clicks();
}

function click_teaser_init(i)
{
	init_teaser(i);
	jQuery('#click'+i).click();stop_scrolling();
}

function switch_teasers()
{
	var timeout = 4000;
	init_teaser(1);
	myint = setInterval("show_teaser()", timeout);
}

function scroll_stage()
{
	myonescroller = setInterval("autoscroll_stage()", 4000);
}

//function show_teaser(id)
function show_teaser()
{
	var alllinks = jQuery(".myteaserlinks");
	var id="#click"+actual_teaser_id;
	jQuery(id).click();
	init_teaser(actual_teaser_id);
	if(actual_teaser_id < (alllinks.length))
		actual_teaser_id=actual_teaser_id+1;
	else
		actual_teaser_id=1;
}

/*
function move_scroller()
{
	$(".scroller").css({"left":"419px"});
	$(".scroller").animate({"left":"0px"},1500);
}
*/


// Teaser
function stop_scrolling()
{
	clearInterval(myint);
}

function init_teaser(i)
{
	
	// koord of stoerer
	var s_x=jQuery("#stoerer_x"+i).attr("value");
	var s_y=jQuery("#stoerer_y"+i).attr("value");
	
	// koord of stripes
	var st_x=jQuery("#stripes_x"+i).attr("value");
	var st_y=jQuery("#stripes_y"+i).attr("value");
	
	var brsr;
	var os;
		
	// Type = "Jetzt Software downloaden", "Jetzt Bild hochladen", "" ...
	var type=jQuery("#type"+i).attr("value");
	
	if(type=='')
		jQuery(".setmytype").css({"display":"none"});
		
	jQuery(".setmytype").attr({"id":type});
	//
	jQuery(".stoerer").css({"display":"none"});
	jQuery("#stoerer"+i).css({"display":"block"});
	
	jQuery(".stripes").css({"display":"none"});
	jQuery("#stripes"+i).css({"display":"block"});
	
	jQuery("#dummy").css({"display":"block"});
	
	// stoerer/stripes offset
	var offset = jQuery("#dummy").position();

	// stoerer positioning
	var s_left = parseInt(s_x) + parseInt(offset.left);
	var s_top = parseInt(s_y) + parseInt(offset.top);

	// stripes positioning
	var st_left = parseInt(st_x) + parseInt(offset.left);
	var st_top = parseInt(st_y) + parseInt(offset.top);
	
	//////////////////
	// IE JS FEHLER DIE INTERNET EXPLORER! BURN IN HELL! 
	// if s_left or s_top is not a number, ie gets stuck, stupid ie
	//jQuery("#stoerer"+i).css({"left":s_left,"top":s_top});
	if(!isNaN(s_left) || !isNaN(s_top))
		jQuery("#stoerer"+i).css({"left":s_left, "top":s_top});
	if(!isNaN(st_left) || !isNaN(st_top))
		jQuery("#stripes"+i).css({"left":st_left, "top":st_top});
	// IE JS FEHLER
	/////////////////

	target=jQuery("#productlink"+i).attr("value");

	if(target == '')
	{
		jQuery(".teaserlink").parent().parent().css({"display":"none"});
		jQuery("#teaserframe").css({"cursor":"auto"});
	}
	else
	{
		jQuery(".teaserlink").parent().parent().css({"display":"none"});
		//$(".teaserlink").parent().parent().css({"display":"block"});
		jQuery("#myteaserlink").attr("href",target);
		var teaserframe = jQuery("#teaserframe");
		teaserframe.css({"cursor":"pointer"});
		teaserframe.click(function() { document.location.href=target; });
	}
	
	jQuery(".myteaserlinks").css({"background-position":"0 0px"});
	
	jQuery("#t"+i).css({"background-position":"0 -21px"});
	return true;
}


function binder()
{
	jQuery(".allmycroppies").bind("mousemove", function(e)
		{		
			jcrop_x = e.pageX;
			jcrop_y = e.pageY;
			jQuery("#myjcrop_info > *").replaceWith("Ausschnitt gespeichert");
		}
	);

}

function calculate_percent_px(width, current, total)
{
	var percent = current / total * 100;
	var current_width = Math.round(width * percent/100);
	return current_width;
}


function get_real_width()
//function show_realdimensions()
{
	return (screen.width - 18);
}


function set_correct_bg()
{
	
	try
	{
		var real_width = get_real_width();
		var content_width = jQuery("#main").width();
		var bg_width = Math.round((real_width - content_width)/2);
		var bg_difference_left = jQuery("#bg-left").width() - bg_width - 21;
		
		/*
		//var bg_left = jQuery("#bg-left").css("background-position");
		// fix for stupid ie
		if(typeof(bg_left) == 'undefined')
		*/

		jQuery("#bg-left").css({"width":(bg_width+21)+"px"});
		jQuery("#bg-left").css({"left":"-"+(bg_width+21)+"px"});
		jQuery("#bg-left").css({"background-position":"-"+bg_difference_left+"px 0"});
		jQuery("#bg-right").css({"width":(bg_width-21)+"px"});

		
		/*
		var real_width = screen.availWidth;
		var fullView = 950 + 375 + 375;
		
		scrollbar_width = screen.availWidth - document.width;
		
		var oversize = Math.round((fullView - real_width)/2);
		
		var bg_width_real = 375 - oversize;
		
		jQuery("#bg-left").css({"width":bg_width_real+scrollbar_width});
		jQuery("#bg-right").css({"width":bg_width_real-scrollbar_width});
		*/
	}
	catch(exception)
	{
		try
		{
			var real_width = screen.availWidth;
			var fullView = 950 + 375 + 375;
			
			var oversize = Math.round((fullView - real_width)/2);
			
			var bg_width_real = 375 - oversize;
			
			jQuery("#bg-left").css({"width":bg_width_real+21});
			jQuery("#bg-right").css({"width":bg_width_real-21});
		}
		catch(exception){}
	}
}

function set_correct_bg_ie8()
{
	var swidth = screen.width;
	var left = ((1700 - swidth) / 2);
	jQuery("#fullbackground-holder").css({"left":"-"+left+"px"});
}


function payment_init()
{
	jQuery(".payment_space > *").bind("click", function(e){ payment_clicked(this.id); });
}

function payment_clicked(id)
{
	//alert(id);
	
	jQuery(".payment-content > *").css({"display":"none"});
	
	if(id=='prepay')
	{
		jQuery("#prepay-info").css({"display":"block"});
		jQuery("#payment-button").removeAttr("class");
		jQuery("#payment-button").attr({"class":"jetztbestellen"});
	}
	else if(id=='creditcard')
	{
		jQuery("#creditcard-info").css({"display":"block"});
		jQuery("#payment-button").removeAttr("class");
		jQuery("#payment-button").attr({"class":"weiter"});
	}
	else if(id='paypal')
	{
		jQuery("#paypal-info").css({"display":"block"});
		jQuery("#payment-button").removeAttr("class");
		jQuery("#payment-button").attr({"class":"weiter"});
	}
}

// delete cartitem -> only upselling + gutschein 
// var s=document.getElementById('quantity_{$article_id}');s[s.selectedIndex].value=0;document.getElementById('aktion_taken').value='delete';document.getElementById('cform').submit();
function delete_from_cart(what)
{
	jQuery("#quantity_"+what).attr({"value":"0"});	
	jQuery("#aktion_taken").attr({"value":"delete"});
	//alert($("#quantity_"+what).attr("value"));
	jQuery("#cform").submit();
}

function click_upload_dialog()
{
	jQuery(".fileupload-filename .file").click();
}

function pa_init()
{
	var img_arr = jQuery(".img-holder > img");
	for(var i=0;i < img_arr.length; i++)
	{
		if(jQuery(img_arr[i]).attr("height")>110)
			jQuery(img_arr[i]).attr({"height":"110"});
	}
	
	jQuery(".pp-image").click(function (){
		release_pa_current();
		var id = jQuery(this).attr("id");
		jQuery(this).parent().parent().parent().addClass("pa-current");
		show_papreview_layer(id);
	});
	
	jQuery(".pp-link-holder .info").click(function (){
		release_pa_current();
		var id = jQuery(this).attr("id");
		jQuery(this).parent().parent().parent().addClass("pa-current");
		show_papreview_layer(id);
	});
	
	// keyword search button
	jQuery("#keyword-search").click(function(){
		pa_keyword_search();
	});
	
	// remove pa-current class if layer is closed
	jQuery("#layer-box-papreview .closer").click(
	function()
	{
		release_pa_current();//$(".pp-article-holder").removeClass("pa-current");
	});
	
	jQuery("#keyword").keydown(function(ev) {
		if(ev.keyCode == 13) { jQuery('#keyword-search').click(); return false;}
		
	} );
	
	pa_set_noresults_height();
	
	pa_change_sizes('poster');
	
}

function pa_change_sizes(material)
{
	jQuery("#pa_sizes").load("/pictureportal/sizes/"+material, function() { initCastomForms(); });
	
}

function pa_preview_submit(id)
{
	var product = jQuery("#pa_product").attr("value");

	jQuery.ajax({
		dataType	: 'text',
		async		: true,
		url			: "/aktion/pa/"+id+"/",
		type		: "POST",
		data		: {"product":product},
		success		: function(data){
			show_pa_completed();
		}
		
	});
	//document.pa_form.submit();
	hide_papreview_layer();
	show_preview_pacart_layer();
}

function vouchergift_add(vid)
{
	

	var giftvoucher 			= "giftvoucher"+vid.toString();
	var thumb_giftvoucher 		= "thumb_giftvoucher"+vid.toString();
	var description_giftvoucher = "description_giftvoucher"+vid.toString();
	var price_giftvoucher 		= "price_giftvoucher"+vid.toString();
	var quantity_giftvoucher	= "quantity_giftvoucher"+vid.toString();
	
		jQuery.ajax({
		dataType	: 'text',
		async		: true,
		url			: "/cart/add/",
		type		: "POST",
		data		: "items[]="+giftvoucher+"&"+quantity_giftvoucher+"=1&"+price_giftvoucher+"="+vid.toString()+"&"+thumb_giftvoucher+"=/images/img_voucher_eur_"+vid+".png&"+description_giftvoucher+"=Geschenkgutschein%20im%20Wert%20von%20"+vid+"%20Euro.",
						/*{
							"items[]"					:	giftvoucher, 
							q_giftvoucher			:	"1", 
							thumb_giftvoucher			:	"/images/img_voucher_eur_"+vid+".png",
							description_giftvoucher 	: 	"Geschenkgutschein im Wert von "+vid+" Euro.",
							price_giftvoucher		:	""+vid+".00"
						},*/
						
		success		: function(data){
			show_vouchercart_completed();
		}
		
	});

	hide_voucher_layer();
	show_vouchercart_layer();
}

function show_pa_completed()
{
	jQuery("#pa_loading").css({"display":"none"});
	jQuery("#pa_loader").css({"display":"none"});
	jQuery("#pa_ready").css({"display":"block"});
	jQuery("#pa_ready_buttons").css({"visibility":"visible"});
}

function show_vouchercart_completed()
{
	jQuery("#vouchercart_loading").css({"display":"none"});
	jQuery("#vouchercart_loader").css({"display":"none"});
	jQuery("#vouchercart_ready").css({"display":"block"});
	jQuery("#vouchercart_ready_buttons").css({"visibility":"visible"});
}

function set_closer_clicks()
{
	// for closing on clicking outside of the layer
	close_on_outerclicks(jQuery(".open-layer > div"));
	
	jQuery(".closer-holder").click(function(){
		// go through all layers and set closed
		hide_me('layer-box-big');
		hide_me('layer-box-huge');
		hide_me('layer-box-middle');
		hide_me('layer-box-middle-fixed');
		hide_me('layer-box-small');
		hide_me('layer-box-small-fixed');
		
	});

}
// for teasers only
function show_softwaredownload_layer()
{
	get_layer_content("big","softwaredownload","blue");
}

function set_layer_links()
{
	jQuery(".layer-show").unbind('click');
	jQuery(".layer-show").click(function()
	{
		
		var page = jQuery(this).attr("id");
		// layersize -> small, small-fixed, big, huge
		// product-details
		if(strpos(page, 'poster-')!==false || strpos(page, 'canvas-')!==false || strpos(page, 'fotobook-')!==false || strpos(page, 'fotocalendar-')!==false || strpos(page, 'acrylglass-')!==false || strpos(page, 'foam-')!==false || strpos(page, 'aludibond-')!==false)
		{
			get_layer_content('small-fixed', page,'blue');
			return true;
		}
		
		
		if(strpos(page,'software-')!==false)
		{
			get_layer_content('middle-fixed', page,'blue');
			return true;
		}
		
		
		if(strpos(page,'foil-')!==false || strpos(page,'pvc-')!==false )
		{
			get_layer_content('middle-fixed', page,'blue');
			return true;
		}
		
		// for fixing warning becausse of same id:
		if(page=='giftvoucher-bnav')
			var page = jQuery(this).attr("id").replace('-bnav','');
		
		if(page=='softwaredownload-bnav')
		{
			var page = jQuery(this).attr("id").replace('-bnav','');
		}

		//webtipps detail, voucherlayer etc.
		var href=jQuery(this).attr("href");		
		if(href=="#webtipps-partner")
		{
			get_layer_content('big', 'webtipps/'+page, 'blue');
			return true;
		}

		switch(page)
		{
			case "disallowvoucher":
				jQuery(".closer-holder").unbind('click');
				set_closer_clicks();
			case "allowvoucher":
				href = href.replace('#','');
				get_layer_content('small', page,'blue',href);
				break;
			case "voucherlayer":
				get_layer_content('small', page,'blue');
				break;
			case "faq":
			case "shipping":
			case "imprint":
			case "agb":
			case "affiliate":
			case "webtipps":
			case "jobs":
			case "contact":
			case "feedback":
			case "giftvoucher":
			case "softwaredownload":
				get_layer_content('big', page,'blue');
				break;
			case "pricelist":
			case "press":
				get_layer_content('huge', page,'blue');
				break;
			case "vouchercart":
				get_layer_content('small-fixed', page,'green');
				break;
			case "designerpro":
				get_layer_content('big', page, 'blue');
				break;
			case "widerruf":
				get_layer_content('big','agb','blue','#Widerrufsrecht');
				break;
			case "multiupload":
				get_layer_content('inv-fixed',page,'blue');
				break;
		}
		//alert(strpos(page, 'poster-'));
		set_closer_clicks();
		return false;
		
		
	});
	// gutscheincode eingabetaste abfangen
	jQuery("#vcode").keydown(function(event) {vcode_enter(event.keyCode);} );
	
	
}

// general hider :)
function hide_me(who)
{
	var me = jQuery("."+who)
	jQuery(me).fadeOut("fast");
	jQuery(me).removeClass("open-layer");
	jQuery("#graymeout").fadeOut("fast");
	
	//$("#layer-box-vouchercart").fadeOut("fast");
}

function set_pp_category_clicks()
{
	if(jQuery(".pp-content")!=null)
	{
		jQuery(".pp-content").click(function(){
			id=jQuery(this).attr("id");
			
			window.location.href = "/pictureportal/show/"+id+"#pachoose";
		});
	}
}

function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strpos    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14    
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

function vcode_enter(ev)
{
	if(ev == 13) { jQuery('#voucherlayer').click(); return false;}
}

function highlight_valentin()
{
	if(jQuery(".lpvalentinstaglink")!=null)
	{
		jQuery(".lpvalentinstaglink").hover(
			function() { 
				jQuery(".lpfotoposter").trigger('mouseenter');  
			} 
		);
	}
}

function move_scroller_to(id)
{
	scroll_cur_product = id;
	
	for(i=0; i<id; i++)
	{
		/*if(i>0)
			setTimeout('$("a.right-btn").click();',1000);
		else*/
			jQuery("a.right-btn").click();
	}
}

function toggle_test_layer()
{
	jQuery("a.right-btn").click();
}

function papreview_size()
{
	jQuery("#previewimage").load(function() 
	{		
		if(jQuery("#previewimage").attr("height")>500)
		{
			jQuery("#previewimage").css({"height":"500px"});
		}
	});
}

function pa_get_surrounding_previews()
{
	// when clicked on preview, class "pa-current" is added to pp-article-holder
	var next = jQuery(".pa-current").next().children().children().filter(':first').text();
	jQuery(".pa-current").next().addClass("pa-current-next");
	var previous = jQuery(".pa-current").prev().children().children().filter(':first').text();
	jQuery(".pa-current").prev().addClass("pa-current-prev");

	if(previous=='')
	{
		previous = jQuery(".pp-list-holder").children().filter(':last').children().children().filter(':first').text();
		jQuery(".pp-list-holder").children().filter(':last').addClass("pa-current-prev");
	}
	if(next=='')
	{
		next = jQuery(".pp-list-holder").children().children().children().filter(':first').text();
		jQuery(".pp-list-holder").children().addClass("pa-current-next");
	}
		
	var retarray = new Array(previous, next);
	return retarray;

	//alert($("#pa_preview_content #preview_title").text());
}

function release_pa_current()
{
	jQuery(".pp-article-holder").removeClass("pa-current");
}

function pa_keyword_search()
{
	var keyword = jQuery("#keyword").val();

	if(keyword.length < 3)
		return false;
		
	keyword=keyword.replace(/\'/g,'');
	keyword=jQuery.URLEncode(keyword);
	
	window.location.href = "/pictureportal/search/"+keyword+"#pachoose";
}

function pa_set_noresults_height()
{
	/*
	var last = $("#sidebar").children().filter(':last');
	var before_last = last.prev();
	
	var last_pos = last.position();
	var before_last_pos = before_last.position();
	//alert($("#sidebar").children().filter(':last').attr("class"));
	var new_height = (last_pos.top + last.outerHeight()) - before_last_pos.top ;
	
	if(last_pos != undefined && before_last_pos != undefined)
		$(".no-results").parent().parent().css({"height":new_height+"px"});
	*/
	var box = jQuery("#sidebar").children().filter(':last').prev();
	jQuery(".no-results").parent().parent().css({"height":box.height()+"px"});
}

function close_on_outerclicks(obj)
{
	jQuery(document).unbind('click');
	jQuery(document).click(function(e){
		if(!is_inner_object(obj, e.pageX, e.pageY))
		{
			hide_me("open-layer");
			obj.removeClass("open-layer");
			jQuery(document).unbind('click');
		}
	});

}

function is_inner_object(obj, x, y)
{
	
	var h = obj.height();
	var w = obj.width();
	
	var top = obj.offset().top;
	var left = obj.offset().left;
	
	var top_max = top + h;
	var left_max = left + w;
	
	//alert("offset: left: "+left+" to "+left_max+"; top: "+top+" to "+top_max+";\n"+"mouse pos: "+x + ", "+y);
	if(x >= left && x <= left_max && y >= top && y <= top_max)
		return true;
	else
		return false;
}

function autoscroll_stage()
{
	// lets see how many products are on stage: 6 are shown, rest is invisible
	var size = jQuery("#carusel ul > li").length - 6; // 3
	
	if(scroll_cur_product >= size)
		autoscroll_dir=-1;

	if(scroll_cur_product < 1)
		autoscroll_dir=1;
	if(jQuery("a.right-btn").hasClass("auto"))
	{
		if(autoscroll_dir>0)
		{
			btnR = jQuery("a.right-btn");
			btnR.removeClass("auto");
			btnR.addClass("auto");
			
			btnR.click();
			scroll_cur_product = scroll_cur_product+1;
		}
		else
		{
			btnL = jQuery("a.left-btn");
			btnL.removeClass("auto");
			btnL.addClass("auto");
			
			btnL.click();
			scroll_cur_product = scroll_cur_product-1;		
		}
	}
}

function stop_scrolling_stage()
{
	clearInterval(myonescroller);
}