/**
 * @author schickedanz
 */



function loadContentFromParameters(Welt, Division, Category, ArticleID, Seite){
	
	if (Division == undefined || Division == '') {
		switchWelt(Welt, $(Welt.toUpperCase()), true);
	}
	else {
		
		if(Welt == 'Air Berlin') Welt = 'Neu';

		var filters = new Array();
		switch (Welt) {
			case 'Sport':
			case 'Damen':
			case 'Herren':
			case 'Kinder':
				filters[0] = 'Welt';
				filters[1] = 'Division';
				filters[2] = 'Category';
				break;
			case 'Neu':
			case 'Sale':
				filters[0] = 'New_Sale';
				filters[1] = 'Welt';
				filters[2] = 'Division';
				break;
			case 'Specials':
				filters[0] = 'Top1';
				filters[1] = 'Action';
				filters[2] = 'SubAction';
				break;
			case 'Luxury Line':
				filters[0] = 'Top3';
				filters[1] = 'Action3';
				filters[2] = 'SubAction3';
				break;
			case 'ABO':
				filters[0] = '';
				filters[1] = 'Welt';
				filters[2] = 'Division';
				break;
		}
		
		
		var URL = Welt != 'ABO' ? SEARCH_URL : SEARCH_URL_ABO;
		if (Welt != undefined && Welt != 'ABO') {
			URL += '&filter' + filters[0] + '=' + Welt;
			Filter[filters[0]] = Welt;
		}
		if (Division != undefined && Division != '') {
			URL += '&filter' + filters[1] + '=' + escape(Division).replace('%26', '%2B');
			Filter[filters[1]] = Division;
		}
		if (Category != undefined && Category != '') {
			if(Category != 'Summer & Holiday')
				URL += '&filter' + filters[2] + '=' + escape(Category).replace('%26', '%2B');
			else
				URL += '&filter' + filters[2] + '=' + escape(Category);
			Filter[filters[2]] = Category;
		}

		if (!document.getElementById('SubNavi') && Welt != this.Welt) {
			switchWelt(Welt, $(Welt.toUpperCase()), false);
		}
		URL = URL.replace(/Falke\//g, 'FalkeTest/');
		if(typeof Seite != 'undefined' && Seite != null)
		 URL = URL + '&page=' + Seite;
		if(typeof mySortBy != 'undefined' && mySortBy != "")
		 URL = URL + '&sort' + mySortBy.split('|')[0] + '=' + mySortBy.split('|')[1];
		if (ArticleID == undefined){
				loadContent(URL, 'makeOverview', 'alert');
			}
		else {
			$('BreadCrumb').show();
			ArticleID = String(ArticleID).replace(/[0]*/, '');
			loadContent(URL + '&filterID=' + ArticleID, 'makeOverview', 'alert');
		}
	}
}

function GetLastOverviewURL(){
	var LastURL = window.name;
	var Suche = /Over:([\s\W\w\S]*)\|/;
	LastURL = Suche.exec(LastURL);
	var Seite = /Page:([\d]*)\|/;
	LastOverPage = Seite.exec(LastURL);
	if (LastOverPage == null) 
		return LastURL[1].replace(/\|[\w\W]*/g, '');
	else {
		return LastURL[1].replace(/\|[\w\W]*/g, '') + '&Page=' + LastOverPage[1];
	}
}

function loadContent(URL, doSuccess, doFailure){

	if(URL.indexOf('filterID') < 0 && doSuccess.indexOf('Over') >= 0)
		window.name = 'Over:' + location.href + '|';	

	if(URL.indexOf('filterID') < 0 && URL.indexOf('page') > 0 && doSuccess.indexOf('Over') >= 0)
		window.name += 'Page:' + URL.replace(/[\w\W]*page=([\d]*)/gi, '$1') + '|';	
	
	if(URL.indexOf('page') < 0)
		window.name.replace(/Page:[\d]*\|/g, '');

	
	if ($('DetailContainer').visible()){ 
		$('DetailContainer').hide();
		DetailIFrame.location.replace('dummy.html');
		$('Content').show();
	}
	URL = URL.replace(/Falke\//g, 'FalkeTest/');

	

	new Ajax.Request(URL, {
		method: 'GET',
		asynchronous: false,
		onSuccess: function(transport){
			response = transport.responseText;
			if (doSuccess != 'eval') {
				eval(doSuccess + "(response)");
			}
			else {
				eval(response);
			}
		},
		onFailure: function(){
			eval(doFailure + '(\'Fehler: ' + URL + '!\');');
		}
	});

			


} 

function formatPrice(Price){
    price = (String(Price).replace(',', '.')).split('.');
	if(price[1] == undefined)
		price[1] = '00';
		return price[0] + ',' + ((price[1].length == 1) ? price[1] + '0' : price[1].substring(0,2));
}

function getParameter(strParameter, strUrl) {
	if(strUrl == undefined) return false;
	arrUrl = strUrl.split("&");  
	for(var p=0;p<arrUrl.length;++p) {
		arrElements = arrUrl[p].split('=');
		if(arrElements[0] == strParameter) {
			return arrElements[(arrElements.length-1)];
		}
	}
	return false;
}

function myStripTags(strNeedle){
	return eval(strNeedle+'.stripTags()');
}


function myTruncate(strNeedle, intChars){
	return eval('String(strNeedle).truncate('+intChars+')');
}


function updateBreadCrumb(objBreadCrumb){
	try {
		var isBackToOverview = false;
		
		var strBreadCrumb = '<p>Sie sind hier: <a href="javascript:void(0);" onmouseup="setHash(new Array(\'camp\', \'afid\', \'gclid\'))">Startseite</a> > ';
		
		try {
			delete this.objMyBreadCrumbs
		} 
		catch (e) {
		}
		this.objMyBreadCrumbs = new Object();
		if (typeof objBreadCrumb.item != 'undefined') 
			$A(objBreadCrumb.item).each(function(BCItem){
				if (BCItem['-nr'] != '0') {
					this.objMyBreadCrumbs['' + String(BCItem['-fieldName']) + ''] = '' + String(BCItem['-value']).replace('+', '&') + '';
				}
			});
		else {
			this.objMyBreadCrumbs = objBreadCrumb;
			isBackToOverview = true;
		}
        
		objBreadCrumb.item = new Array(objBreadCrumb.item);
		
		
        if (location.hash.indexOf('search') < 0) {
			if (typeof this.objMyBreadCrumbs['aboFlag'] != 'undefined' && this.objMyBreadCrumbs['aboFlag'] == '1') {
				this.objMyBreadCrumbs['Division'] = this.objMyBreadCrumbs['Welt'];
				this.objMyBreadCrumbs['Welt'] = 'ABO';
			}
			if (typeof this.objMyBreadCrumbs['New_Sale'] != 'undefined') {
				this.objMyBreadCrumbs['Division'] = this.objMyBreadCrumbs['Welt'];
				this.objMyBreadCrumbs['Welt'] = this.objMyBreadCrumbs['New_Sale'];
			}
			if (typeof this.objMyBreadCrumbs['Top1'] != 'undefined') {
				this.objMyBreadCrumbs['Division'] = this.objMyBreadCrumbs['Action'];
				this.objMyBreadCrumbs['Welt'] = this.objMyBreadCrumbs['Top1'];
			}
			if (typeof this.objMyBreadCrumbs['Top3'] != 'undefined') {
				this.objMyBreadCrumbs['Division'] = this.objMyBreadCrumbs['Action3'];
				this.objMyBreadCrumbs['Welt'] = this.objMyBreadCrumbs['Top3'];
			}
			if (typeof this.objMyBreadCrumbs['Welt'] != 'undefined' || typeof this.objMyBreadCrumbs['Top3'] != 'undefined' || typeof this.objMyBreadCrumbs['Top1'] != 'undefined') {
				if (($('OverViewHeader') || $('DetailIFrame').visible()) && !isBackToOverview) {
					$('BreadCrumb').update(strBreadCrumb + '<a href="javascript:void(0)" onmouseup="setHash(\'\', \'welt=' + this.objMyBreadCrumbs['Welt'] + '\')">' + this.objMyBreadCrumbs['Welt'].replace(/[^0-9a-zA-Z\s]*/g, '') + '</a> > <a href="javascript:void(0)" onmouseup="setHash(\'\', \'welt=' + this.objMyBreadCrumbs['Welt'] + '&division=' + this.objMyBreadCrumbs['Division'] + '\')">' + this.objMyBreadCrumbs['Division'] + '</a></p>');
					document.title = 'FALKE Online-Shop - ' + this.objMyBreadCrumbs['Division'];
				}
				else {
					$('BreadCrumb').update(strBreadCrumb + this.objMyBreadCrumbs['Welt'].replace(/[^0-9a-zA-Z\s]*/g, '') + '</p>');
					document.title = 'FALKE Online-Shop - ' + this.objMyBreadCrumbs['Welt'].replace(/[^0-9a-zA-Z\s]*/g, '');
				}
			}
		}
		else {
			$('BreadCrumb').update(strBreadCrumb + ' Produktsuche</p>');
			document.title = 'FALKE Online-Shop - Produktsuche';
		}
		var intI = 0;
		
		//SubNavi zurücksetzen
		$A($$('td.subnaviitem')).each(function(SubNaviItem){
				SubNaviItem.className = SubNaviItem.className.replace(' subon', '');
				SubNaviItem.onmouseover = function(){SubNaviItem.className = SubNaviItem.className + ' subon'};
				SubNaviItem.onmouseout  = function(){SubNaviItem.className = SubNaviItem.className.replace(' subon', '')};

			if (SubNaviItem.firstChild.nodeValue.toLowerCase() == this.objMyBreadCrumbs['Division'].toLowerCase() && !isBackToOverview) {
				SubNaviItem.className = SubNaviItem.className + ' subon';
				SubNaviItem.onmouseover = function(){};
				SubNaviItem.onmouseout = function(){};
			}
			intI++;
		});
	} 
	catch (e) {}
	$('BreadCrumb').show();
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)            
  endstr = document.cookie.length;	               
  return unescape(document.cookie.substring(offset, endstr));
}
	                
function GetCookie(name){
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var tCookie=document.cookie;
  var i = 0;
  while (i < clen) 
  {
    var j = i + alen;
    if(tCookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = tCookie.indexOf(" ", i) + 1;
    if (i == 0) 
      break; 
  }
  return "";
}

function SetCookie(name,value,expires){
  var AddStr="; path=/";// Schränkt ein
  AddStr="";

  if(expires!=null)
    AddStr=AddStr+"; expires=" + expires.toGMTString();
  document.cookie = name + "=" + escape (value) +AddStr;
}

//Höhe und Breite des/der FaderDivs(Hinweislayer + Bestätigungslayer) bestimmen
function getPageSize(obj, blnScroller){
	if(blnScroller == undefined) blnScroller = true;
	
	if(obj == undefined) obj = this;
	var xScroll, yScroll;
	if (obj.innerHeight && obj.scrollMaxY) {
		xScroll = obj.document.body.scrollWidth;
		yScroll = obj.innerHeight + obj.scrollMaxY;
	} else if (obj.document.body.scrollHeight > obj.document.body.offsetHeight){ // all but Explorer Mac
		xScroll = obj.document.body.scrollWidth;
		yScroll = obj.document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = obj.document.body.offsetWidth;
		yScroll = obj.document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (obj.innerHeight) {	// all except Explorer
		windowWidth = obj.innerWidth;
		windowHeight = obj.innerHeight;
	} else if (obj.document.documentElement && obj.document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = obj.document.documentElement.clientWidth;
		windowHeight = obj.document.documentElement.clientHeight;
	} else if (obj.document.body) { // other Explorers
		windowWidth = obj.document.body.clientWidth;
		windowHeight = obj.document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	pageWidth = (obj.document.body.clientWidth < 826 && !document.all && blnScroller) ? (pageWidth - 16) : pageWidth;
	
	if(!document.all)
		pageWidth = (pageHeight > windowHeight) ? (pageWidth - 16) : pageWidth;
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	
	return arrayPageSize;
}

function parseToBreadCrumb(arrNeedle, arrFields){
	var strOut  = '{"item": [{"-nr": "0", "-type": "search", "-value": "*", "searchParams": "/FalkeTest/Search.ff?query=*&channel=de&productsPerPage=12&xml=true"}';
		strOut += ', {"-nr": "1", "-type": "filter", "-value": "'+arrNeedle[1][arrFields[0]]+'", "-fieldName": "Welt", "searchParams": "/FalkeTest/Search.ff?query=*&filterWelt='+arrNeedle[1][arrFields[0]]+'&channel=de&productsPerPage=12&xml=true"}';
		strOut += ', {"-nr": "2", "-type": "filter", "-value": "'+arrNeedle[1][arrFields[1]]+'", "-fieldName": "Division", "searchParams": "/FalkeTest/Search.ff?query=*&filterWelt='+arrNeedle[1][arrFields[0]]+'&filterDivision='+arrNeedle[1][arrFields[1]]+'&channel=de&productsPerPage=12&xml=true"}';
		strOut += ']}';
	
	return eval("(" + strOut + ")"); 
}

function setHash(arrKeeptheseElements, addElement){
	try {
		pQueryInput = document['search_word_inc']['query'];
		
		if(pQueryInput.style.backgroundImage == 'none'){
			pQueryInput.style.backgroundImage = 'url(../Shop_Relaunch/Img/Header/de/searchbar_bg.gif)';
			pQueryInput.style.color = '#CCCCCC';
		}
		
		if(arrKeeptheseElements == undefined)
		
			//arrKeeptheseElements = new Array('welt', 'division', 'category', 'camp', 'gclid', 'afid', 'utm_source', 'utm_medium', 'utm_term');
			arrKeeptheseElements = new Array('welt', 'division', 'category', 'camp', 'gclid', 'afid');
		else{
			if(typeof arrKeeptheseElements == 'string')
				arrKeeptheseElements = new Array();
				
			arrKeeptheseElements[arrKeeptheseElements.length] = 'camp';
			arrKeeptheseElements[arrKeeptheseElements.length] = 'gclid';
			arrKeeptheseElements[arrKeeptheseElements.length] = 'afid';
/*
			arrKeeptheseElements[arrKeeptheseElements.length] = 'utm_source';
			arrKeeptheseElements[arrKeeptheseElements.length] = 'utm_medium';
			arrKeeptheseElements[arrKeeptheseElements.length] = 'utm_term';
*/			
		}
		
		var arrNewHash = new Array();
		if (location.hash != '' && arrKeeptheseElements != null) {
			arrCurrentHash = GetHash(location.hash).split('&');
			$A(arrCurrentHash).each(function(HashElement){
				$A(arrKeeptheseElements).each(function(keeper){
					if (HashElement.indexOf(keeper + '=') == 0) 
						arrNewHash[arrNewHash.length] = HashElement;
				});
			});
		}
		if (addElement != undefined) {
			arrNewHash[arrNewHash.length] = addElement;
		}
		if (location.hash != arrNewHash.join('&')) {
			location.hash = arrNewHash.join('&');
			if((location.hash.indexOf('&') < 0 && location.hash.indexOf('camp=') >= 0) || (location.hash.indexOf('&') > 0 && (location.hash.indexOf('camp=') >= 0 && location.hash.indexOf('gclid=') >= 0 && location.hash.indexOf('welt=') < 0 && location.hash.indexOf('goto=') < 0 && location.hash.indexOf('search=') < 0)))
				location.reload();
			
			
			if (HiddenIFrame && window.ActiveXObject){ 
				HiddenIFrame.location.search = '?' + arrNewHash.join('&');
			}
		}
	}catch(e){}
}

function checkKey(myEvent, objEventTarget){
		if(!myEvent)
		myEvent = window.event;
	if (myEvent.keyCode == 13) {
		if(objEventTarget != undefined)
			objEventTarget.blur();
		return;
	}
}

function replaceUmlaute(strNeedle)
{
	var strNewString = "";
	
	for(var i = 0; i <= strNeedle.length; i++){
		switch(strNeedle.charAt(i)){
			case 'ä':
				strNewString += 'ae';
				break;		
			case 'ü':
				strNewString += 'ue';
				break;		
			case 'ö':
				strNewString += 'oe';
				break;		
			case 'ß':
				strNewString += 'ss';
				break;
			default:
				strNewString += strNeedle.charAt(i);
				break;
		}
	}
	return strNewString;
}





