jQuery.fn.exists = function(){return jQuery(this).length>0;}

var rubrik = 1;

// alle links der navigation so binden das lblock die navigation per ajax laden wird und der content entsprechend aktualisiert wird
$(document).ready(function () {
	PMbindNavLinks();
	bindDetails();
	bindPaginator();
	PMbindPathInContent();
	$("div.imgpane a").lightbox();
	// webeinterval
	window.setInterval(function(){ladeSeitenbanner();},12000);

/*
	$("a","#cnav").click( function() {
		var fileName = $(this).attr("href");
		pageTracker._trackPageview(fileName);
		return true;
	});
*/	
	

});
function ladeSeitenbanner(){

	$('#seitenbanner').load('/ajax.php?package=ch.iframe.snode.banner&module=related&param_sourcetype=1201&param_sourceid=' + rubrik + '&param_zone=4&param_style=sidebanner.tpl',function(){});
	$('#skyscraper').load('/ajax.php?package=ch.iframe.snode.banner&module=related&param_sourcetype=1201&param_sourceid=' + rubrik + '&param_zone=7&param_style=sidebanner.tpl',function(){});
}
function PMbindNavLinks(){

	$("#cnav a").bind("click", function(){
		nodeID = $(this).attr('rel');
		if(nodeID > 0){
			$('#lblock').load('/ajax.php?package=ch.iframe.snode.catalog&module=category_tree&param_open_depth=2&param_node=1&x1200_node=' + nodeID,function(){rfrshCntnt(nodeID);PMbindNavLinks();});
			
			return false;
		}
	});
	$("#cnavselector a").bind("click", function(){
		nodeID = $(this).attr('rel');
		$('#lblock').load('/ajax.php?package=ch.iframe.snode.catalog&module=category_tree&param_open_depth=2&param_node=1&x1200_navmode=' + nodeID,function(){PMbindNavLinks();});
		return false;
	});

}
function PMbindPathInContent(){
	$("div.path > a").bind("click", function(){
		$('#lblock').load('/ajax.php?package=ch.iframe.snode.catalog&module=category_tree&param_open_depth=2&param_node=1&x1200_node=' + $(this).attr('rel'),function(){rfrshCntnt($(this).attr('rel'));PMbindNavLinks();});
		return false;
	});
}

// von Kurzansicht auf die Grossansicht wechseln
function changeViewMode(viewmode,nodeID){
	$('html, body').animate({scrollTop:0}, 'slow');
	$('#inserate').fadeTo(100,0.5);
	$('#inserate').load('/ajax.php?package=ch.iframe.snode.catalog&module=PM09_product_list&param_show_fields=3,4,5,6,7,8,9,10,11,12,13&x1200_page=1&x1200_node=' + nodeID + '&x1200_viewmode=' + viewmode,
	function(){
		$('#inserate').fadeTo(700,1);
		// thickbox binden
		$("div.imgpane a").lightbox();
		bindDetails();
		bindPaginator(nodeID);
		PMbindPathInContent();
	}
	);
}

// von Kurzansicht auf die Grossansicht wechseln
function changeItemsPerPage(perpage,nodeID){
	$('html, body').animate({scrollTop:0}, 'slow');
	$('#inserate').fadeTo(100,0.5);
	$('#inserate').load('/ajax.php?package=ch.iframe.snode.catalog&module=PM09_product_list&param_show_fields=3,4,5,6,7,8,9,10,11,12,13&x1200_page=1&x1200_node=' + nodeID + '&x1200_perpage=' + perpage,
	function(){
		$('#inserate').fadeTo(700,1);
		// thickbox binden
		$("div.imgpane a").lightbox();
		bindDetails();
		bindPaginator(nodeID);
		PMbindPathInContent();
	}
	);
}

function rfrshCntnt(nodeID){
	changeHeadBanner(nodeID)
	rubrik = nodeID;
	pageTracker._trackPageview('Rubrik ' + nodeID );
	if($('.inseratdisplay').exists()){
		$('html, body').animate({scrollTop:0}, 'slow');
		$('#inserate').fadeTo(100,0.5);
		$('#inserate').load('/ajax.php?package=ch.iframe.snode.catalog&module=PM09_product_list&param_show_fields=3,4,5,6,7,8,9,10,11,12,13&x1200_page=1&x1200_reset_filter=true&x1200_node=' + nodeID ,
		function(){
			$('#inserate').fadeTo(700,1);
			// thickbox binden
			$("div.imgpane a").lightbox();
			bindDetails();
			bindPaginator(nodeID);
			PMbindPathInContent();
		}
		);
	}else{
		window.location ='/index.php?TPL=10105';
	}
}

function bindPaginator(nodeID){
	$(".paginator a").bind("click", function(){
		$('html, body').animate({scrollTop:0}, 'slow');
		$('#inserate').fadeTo(100,0.5);
		$('#inserate').load('/ajax.php?package=ch.iframe.snode.catalog&module=PM09_product_list&param_show_fields=3,4,5,6,7,8,9,10,11,12,13&x1200_page=' + $(this).attr('rel') + '&x1200_node=' + nodeID ,
		function(){
			$('#inserate').fadeTo(700,1);
			// thickbox binden
			$("div.imgpane a").lightbox();
			bindDetails();
			bindPaginator(nodeID);
			PMbindPathInContent();
		}
		);
		return false;
	});
	initAutosearch();
}

function changeHeadBanner(nodeID){
	$('#headbanner').load('/ajax.php?package=ch.iframe.snode.banner&module=related&param_sourcetype=1201&param_version=7&param_zone=1&param_style=head.tpl&param_sourceid=' + nodeID );

	$('#visis').load('/ajax.php?package=ch.iframe.snode.articles&module=related&param_source_type=1201&param_source_id=' + nodeID );

}

function bindDetails(){
	// detailanzeige binden
	$("a.details").bind("click", function(){
		$('html, body').animate({scrollTop:$('#i' + $(this).attr('rel')).offset().top}, 'slow');
		var inserat = $(this).attr('rel');
		$('#i' +  inserat).load('/ajax.php?package=ch.iframe.snode.catalog&module=PM09_product_list_details&param_show_fields=3,4,5,6,7,8,9,10,11,12,13&x1200_id=' + $(this).attr('rel'),
		function(){
			$(this).css('border-color','#EE3333');
			$('#i' +  inserat + ' div.imgpane a').lightbox();
		});
		return false;
	});

	$("a.merkliste").bind("click", function(){
		pageTracker._trackPageview('Merkliste');
		$('#merkliste').load('/ajax.php?package=ch.iframe.snode.catalog&module=PM09_merkliste&x1200_zurmerkliste=' + $(this).attr('rel'));
		return false;
	});
	

}


function initAutosearch(){
	$('#extsrchAuto').bind('click',function(){
		$('#suchoptionen').slideUp().slideDown('slow').load('/ajax.php?package=ch.iframe.snode.catalog&module=PM09_autofilter',
		function(){
			//submitter initialisieren
			initExtSrch();
			bindPaginator();
		}
		);
		return false;
	});
	$('#extsrchErotik').bind('click',function(){
		$('#suchoptionen').slideUp().slideDown('slow').load('/ajax.php?package=ch.iframe.snode.catalog&module=PM09_erotikfilter',
		function(){
			//submitter initialisieren
			initExtSrch();
			bindPaginator();
		}
		);
		return false;
	});

	$("#autosearch").autocomplete('/ajax.php?package=ch.iframe.snode.search&module=PM09search',
	{ width: "inherit"
	,minChars:2
	,extraParams: {"x80_node":$('#asnode').val() }
	,max: 15
	,delay: 300
	,dataType: "json"
	,parse: autocompleteJSON
	,formatItem: function(row) {
		var zeile;
		zeile = '<span>' + row["date"] + '</span><br />';
		if(row["image"] > 0){
			zeile = zeile + '<img src="/download.php?file_id=' + row["image"] + '&file_version=0">';
		}
		zeile = zeile + '<h1>' + row["title"] + '</h1><br />' + row["desc"];

		return zeile }
		,mustMatch: false
		,selectFirst: true
	}).result(function(event, data, formatted){
		if( data["id"]){
			window.location = '/index.php?TPL=10104&x1200_id=' + data["id"];
		}
	});

}

function initExtSrch(){
	$('#extended_search').submit(function(){
		var dataarray = new Array();
		$('#extended_search :input').each(function(){
			if($(this).attr('name') != ''){
				dataarray[$(this).attr('name')] = $(this).val();
			}
		});

		var datastring = '';
		for (key in dataarray){
			datastring = datastring + '&' + key + '=' + dataarray[key];
		}

		$('#inserate').fadeTo(100,0.5);
		$('#inserate').load('/ajax.php?package=ch.iframe.snode.catalog&module=PM09_product_list&param_show_fields=3,4,5,6,7,8,9,10,11,12,13'+ datastring,
		function(){
			$('#inserate').fadeTo(700,1);
			// thickbox binden
			$("div.imgpane a").lightbox();

			PMbindPathInContent();
			bindDetails();
			//submitter initialisieren
			initAutosearch();


		}

		);


		return false;
	});

}

