// JavaScript Document



function MM_changeProp(objId,x,theProp,theValue) { //v9.0
  var obj = null; with (document){ if (getElementById)
  obj = getElementById(objId); }
  if (obj){
    if (theValue == true || theValue == false)
      eval("obj.style."+theProp+"="+theValue);
    else eval("obj.style."+theProp+"='"+theValue+"'");
  }
}

// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  if(!theDoc) theDoc = document;
  
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];

  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);

  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

  return foundObj;
}


function showDesc(id1, id2){
	
	var block1 = findObj(id1);
	var block2 = findObj(id2);
	
	block1.style.display = 'inline';
	block2.style.display = 'none';
}

function change_content(id,content){
	var obj = findObj(id);
	//var obj = document.getElementById(id);
	//if (obj == undefined ) return false;
	obj.innerHTML = content;
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height) {

  if(left == 1) left = (screen.width/2) - width/2;
  if(top == 1) top = (screen.height/2) - height/2;

  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  if(popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'')){
	  	popUpWin.focus();
  } else {
		alert('Diese Seite wird in einem PopUp Fenster angezeigt.\nDeaktivieren Sie bitte Ihren PopUp Blocker.');  
  }
}


// Filter-Funktionen

/*
function checkSetForm(form) {
  var checkflag = "false";
  for (i = 0; i < form.elements["ft[]"].length; i++) {
	if (form.elements["ft[]"][i].selectedIndex > 0) checkflag = "true";
  }
  if(checkflag == "true") form.sendButton.disabled=false;
  else form.sendButton.disabled=true;
}
*/

// Filter setzen
function sendFilter(ft,basisurl){
	
	var form = document.formFilter;
	var arrTemp = new Array();
	var k = 0;
	var idx = 0;
	var hiddenStr = '';
	var magick = 0;
	
	for (i = 0; i < form.elements["ft[]"].length; i++) {
		idx = form.elements["ft[]"][i].selectedIndex;
		if ( idx > 0) {
			//params += form.elements["ft[]"][i].options[idx].value;
			arrTemp.push(form.elements["ft[]"][i].options[idx].value);
		}
	}
	
	var nextUrl = basisurl;
	
	for (i = 0; i < form.elements.length; i++) {
		
		if(form.elements[i].type != 'hidden') continue;
		if(magick == 0) magick = 1; else hiddenStr += '&';
		hiddenStr += (form.elements[i].name + '=' + form.elements[i].value);
	}
	
	if(hiddenStr != '') nextUrl += '?'+hiddenStr;
	
	if( ft != '' || arrTemp.length > 0) {
		nextUrl += (pagetype == 'query' || hiddenStr != '') ? '&' : '?';
		nextUrl += 'ft=';
	}
	if( ft != '') nextUrl += ft;
	if( ft != '' && arrTemp.length > 0) nextUrl += ',';
	if( arrTemp.length > 0) nextUrl += arrTemp.join(',');

	self.location =  nextUrl;
}
/*
function check(form) {
	var checkflag = "false";
	for (i = 0; i < form.length; i++) {
		if( form[i].type == 'checkbox') { if (form[i].checked == true) checkflag = "true";}
	}
	if(checkflag == "true") form.sendButton.disabled=false;
	else form.sendButton.disabled=true;
}
*/

// Filter löschen
function deleteFilter(ft){
	
	// URL zerteilen
	var splittedUri = window.location.href.split('?');
	
	// Seitennummer in Kategorie-URLs auf 1 setzen
	var basisurl =  splittedUri[0].replace(/[0-9]+\.html/,'1.html');
	
	// pagetyp feststellen
	var pagetype = basisurl.indexOf('.php') > 0 ? 'query' : 'browse';
	
	var paramsArr = splittedUri[1].split('&');
	
	
	var params = '';
	var myCount = 0;
	
	for(var i = 0; i < paramsArr.length; ++i){
		
		
		// Filter-Parameter ausfiltern: 
		
		if( paramsArr[i].match(/^ft/) ){
			
			// Alle Filter löschen
			if(ft == 'all') continue;
			
			// weiter mit einzelnen Filter löschen
			var ftArr =  paramsArr[i].split('=');
			
			if( ftArr[1].indexOf(',') > 0 ) {
				
				var newValuesArr = new Array();
				
				var ftValuesArr = ftArr[1].split(',');
				
				for(var ii = 0; ii < ftValuesArr.length; ++ii){
					
					if( ftValuesArr[ii] == ft ) continue;
					
					newValuesArr.push(ftValuesArr[ii]);
				}
				
				if(myCount == 0) myCount = 1; else params += '&';
				params += 'ft='+newValuesArr.join(',');
				
			}
			
		} else {
			
			// Seiten-Nummer bei Suche löschen
			if(paramsArr[i].match(/^pgn/)) continue;
			
			// Alle weiteren Queries unverändert dranhängen
			if(myCount == 0) myCount = 1; else params += '&';
			params += paramsArr[i];
						
		}
		
	}

	
	//if(hiddenStr != '') nextUrl += '?'+hiddenStr;
	
	var paramsDeli = pagetype == 'query' ? '&' : '?';
	
	if(params != '') basisurl += paramsDeli+params;
	
	self.location =  basisurl ;
}

function watchlist(pid,typ,a){
	
	$('#menuWatchListContent').load('/shopping/handler_watchlist.php?id='+pid+'&typ='+typ+'&a='+a);
	if(a=='add') $('#watchitem'+pid).html('<img src="/images/icons/merkliste-entfernen.jpg" width="20" height="20" alt="" class="icon" /><a href="javascript:watchlist(\''+pid+'\',\''+typ+'\',\'remove\')" class="inlist">Aus Merkliste löschen</a>');
	else $('#watchitem'+pid).html('<img src="/images/icons/merkliste-icon.jpg" width="20" height="20" alt="" class="icon" /><a href="javascript:watchlist(\''+pid+'\',\''+typ+'\',\'add\')">Auf die Merkliste</a>');
	//$('#watchitem'+pid).addClass('text-grau');
	
}


// Funktion wird direkt in der Merkliste benötigt - lädt die Seite neu
function watchlist_remove(id,typ){
	
	$.get('/shopping/handler_watchlist.php', { 'id':id, 'typ':typ, 'a':'remove' }, function(data) {
		
		if(data != ''){
			
			location.reload();
		
		} else {
			
			alert('Es ist ein Fehler aufgetreten, versuchen Sie es bitte noch einmal');
			
		}
	});
	
}

function comparelist(cid,pid,action,sid,sname){
	
	$.get('/shopping/handler_comparelist.php', { 'pid':pid, 'cid':cid, 'a':action, 'sname':sid }, function(data) {
	  if(data == 'full') {
		  alert('Aus Gründen der Übersicht können nur 3 Produkte gleichzeitig innerhalb einer Kategorie verglichen werden!\n\nUm ein neues Produkt hinzuzufügen, löschen Sie bitte zuerst ein Produkt dieser Kategorie aus dem Vergleich.');
	  } else {
	  	$('#menuCompareListContent').html(data);
		if(action == 'add') $('#compareitem'+pid).html('<img src="/images/icons/produktvergleich-entfernen.jpg" width="20" height="20" alt="" class="icon"/><a href="javascript:comparelist(\''+cid+'\',\''+pid+'\',\'remove\',\''+sid+'\',\''+sname+'\')" class="inlist">Aus Vergleich löschen</a>');
		else $('#compareitem'+pid).html('<img src="/images/icons/produktvergleich-icon.jpg" width="20" height="20" alt="" class="icon"/><a href="javascript:comparelist(\''+cid+'\',\''+pid+'\',\'add\',\''+sid+'\',\''+sname+'\')">Produkt vergleichen</a>');
		//$('#compareitem'+pid).addClass('text-grau');
	  }
	});
}

// Funktion wird direkt im Vergleich benötigt - lädt die Seite neu
function comparelist_remove(cid,pid){
	
	$.get('/shopping/handler_comparelist.php', { 'pid':pid, 'cid':cid, 'a':'remove' }, function(data) {
		
		if(data.match(/^Fehler/)){
			
			alert(data);
		
		} else if(data == ''){
			
			alert('Es ist ein Fehler aufgetreten, versuchen Sie es bitte noch einmal');
			
		} else {
			
			location.reload();
			
		}
	});
	
}


function drucken() { print(document); }


function toggleDisplay(id,id_more){
	
	
	if($('#'+id).css("display") == "none"){
		
		$('#'+id).animate({
  			
			"height": "toggle", "opacity": "toggle"

		}, "slow");
		
		$('#'+id).fadeIn('slow');
		
		$('#'+id).css("display","inline");

		$('#'+id_more).text('weniger');
	
	} else {
		
		
		$('#'+id).animate({
  			"height": "toggle", "opacity": "toggle"

		}, "slow");	
		
		$('#'+id).fadeOut('slow');
		
		$('#'+id_more).text('mehr');
		
	}
	
}




$(document).ready(function() {
	
	// autocomplte v. 2
	/*
	$('#iqry').autocomplete({ 
		serviceUrl:'/shopping/autocomplete.php', 
		minChars:2, 
		width:436 
	});
	*/
	
	// autocomplte v. 1
	
	$("#iqry").autocomplete("/shopping/autocomplete.php", { minChars:3, width:436, lineSeparator:'|' });
	
	$('a[rel*=facebox]').facebox();
	
	$("#bt2").css("display","none");  // Text Teil 2 unter dem Footer verbergen
	
	
	// Hilighting //
	var aHover = 0;
	
	$("a").hover(function(){ aHover = 1; } , function(){ aHover = 0; } ); // ist der Cursor über einem <a href...> ?
	
	$('div.hlt').mouseover(function() {
	  $(this).addClass('highlightForClick');
	});
	$('div.hlt').mouseout(function() {
	  $(this).removeClass('highlightForClick');
	});
	
	
	// *****   Group Fader  ****//
	
	//$('.button-grouped a').hover(function(){ alert(aHover) });
	
	$('.button-grouped a').click(function(){
		
		//var thisDiv = $(this).parent();
		var thisButton = $(this);
		var merchantId = $(this).attr('rel');
		
		//alert(merchantId);
		
		if($('#offer_group_' + merchantId).css('display') == 'none'){
			
			$('#offer_group_' + merchantId).slideDown('slow', function(){ 
				$(thisButton).html('<img src="/images/icons/minus.gif" class="icon" />weniger Angebote');
			});
			
		} else {
			
			$('#offer_group_' + merchantId).slideUp('slow', function(){ 
				$(thisButton).html('<img src="/images/icons/plus.gif" class="icon" />mehr Angebote');
			});
			
		}
		
		return false;
		
	});
	
	
	if(aHover == 0){
	
		$('div.hlt').click(function() {

		  window.open($(this).find("a.jqShoplink").attr("href"));
		  return false;
	
		});
	
	}
	// Ende Hilighting //
	
	
	
});

