$(document).ready(function() {  
	 
	 initializeImageChanger();
	 
	 initializeResetCheckboxListener();
	
	 initializeFacettedSearchCheckboxListener();
	 
	 setMinWidthOnSelectField();
	 
})

 
 /**
  * the mainProductPicture should be in the html-format: <a href="<zoompicture>"><img src="<mainpicture"/></a>
  * the pictureSelectorLink should be in the html-format: <a href="<mainpicture>"><img></a>
  * 
  * use id #mainProductPicture for the Product Picture to change.
  * use class pictureSelectorLink for the selector-Links.
  * @return
  */
function initializeImageChanger() {
	$("a.pictureSelectorLink").click(
			function(event){
				//change the mainProductPicture
				event.preventDefault();
				var defaultPictureSRC = $(this).attr("href");
				$("#mainProductPicture").attr("src", defaultPictureSRC);
				
				//change the class of the selectorpictures
				//var pictureSelectorImages = $("a.pictureSelectorLink").find("img");
				var pictureSelectorImages = $("a.pictureSelectorLink");
				pictureSelectorImages.removeClass("act");
				//var selectedImage = $(this).find("img");
				var selectedImage = $(this);
				selectedImage.addClass("act");
				
				//add the zoompicture-src
				var zoomPictureSRC = $(this).find("span").text();
				var zoomPictureLinkElement = $("#mainProductPicture").parent("a");
				zoomPictureLinkElement.attr("href", zoomPictureSRC);
				
				//jqzoom: load new largeimage
				myJqzoomObject.changeLargeImageUrl(zoomPictureSRC);
			}
	);
}
 

function changeUrl(selectDropdown) {
	 var selectedIndex = $(selectDropdown).attr("selectedIndex");
     if(selectedIndex >= 0) {
    	 window.location = $(selectDropdown).val();
     }	
}


function initializeResetCheckboxListener() {
	//reset the checkboxes if reset-button is clicked
	$(".resetSelection").live("click",function(event){
		event.preventDefault();
		$('#watchfinderSearchform :checkbox').attr('checked', false);
		getFacetedSearchResult();
	});
}

function initializeFacettedSearchCheckboxListener() {
	$("#watchfinderSearchform :checkbox").click(function(){
		getFacetedSearchResult();
	});
}

function getFacetedSearchResult() {
	$.ajaxSetup ({
		cache: false
	});
	var ajax_load = "<div class='ajax-loading'><img src='/images/ajax/big_circle_ball_red.gif' alt='loading...' /></div>";
//	load() functions
	var loadUrl =  window.location.pathname;
	var queryString = $("#watchfinderSearchform").serialize();
	$("#watchfinderResultarea").html(ajax_load).load(loadUrl + "?" + queryString + " #watchfinderResultarea",function(){
		initializeCufon();
	});
}

//used for pagination-calls
function makeAjaxCall(link) {
	$.ajaxSetup ({
		cache: false
	});
	var ajax_load = "<div class='ajax-loading'><img src='/images/ajax/big_circle_ball_red.gif' alt='loading...' /></div>";
//	load() functions
	var loadUrl =  window.location.pathname;
	
	var queryStringForm = $("#watchfinderSearchform").serialize();
	var paginationLinkUrl = $(link).attr("href");
	var queryStringPagination = extractQueryString(paginationLinkUrl);
	var finalQueryString = queryStringForm + "&" + queryStringPagination;
	$("#watchfinderResultarea").html(ajax_load).load(loadUrl + "?" + finalQueryString + " #watchfinderResultarea",function(){
		initializeCufon();
	});
	return false;
}

function extractQueryString(wholeUrl) {
	var arrUrl = wholeUrl.split("?");
	if(arrUrl.length==2) {
		var queryString = arrUrl[1];
		return queryString;
	}
	return "";
}

/**
 * set the high of the footer
 */
function setFooterHigh(){
	
	 footer = $(".footerWrap");
	 var footerPosition = footer.position();
	 
	 if($(document).height() <= ($(window).height()+footer.height())){
		 var footerHighDif = $(window).height() - (footerPosition.top + 30);
		 footer.css("height", footerHighDif + "px");
	 }
}

/**
 * select min-width for dropdown on product detail page. (ie 6 and 7 can't use the css comment min-widht on select field)
 */
function setMinWidthOnSelectField(){
	
	var dropdownElements = ["#sizeDropdown","#colorDropdown"];
	var minWidth = 150;
	
	$.each(dropdownElements, function(index, element) { 
		width = $(element).width();
		
		if(width <= minWidth){
			$(element).css("width", minWidth + "px");
		}
		
	});
}

/**
 * Special Flyout menu
 */

function getLayer( strID )
{
	if( document.all && !window.opera )
		return document.all[ strID ];
	else
		return document.getElementById( strID );
}

function getClassName(element) {
	if(element == null) {
		return "";
	}
	var className = document.all && !window.opera ? element.className : element.getAttribute("class");
	return className == null ? "" : className;
}

sfHover = function() {
	
	if(document.getElementById("subnavigation")){

		var sfEls = document.getElementById("subnavigation").getElementsByTagName("LI");
		
		
	
		if(sfEls){
		
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


// Class Flyout, Constructor
function Flyout(/*String*/ id, /*int*/ type) {
	this.id = id;
	this.type = type;
	this.active = false;

	// Get Layer for this Flyout
	if (id) { this.flyout = getLayer(id);}

	// Check and extend global Flyout list if necessary
	if (!Flyout.flyouts[type]) {Flyout.flyouts[type] = new Array();}

	// Add flyout to global Flyout object
	Flyout.flyouts[type][id] = this;
	
	// Add event listener for this flyout
	if (this.flyout) {
		addEvent(this.flyout, "mouseover", function() {Flyout.flyouts[type][id].activate() }, true);
		addEvent(this.flyout, "mouseout", function() { Flyout.flyouts[type][id].deactivate() }, true);
	}
}

var /*Boolean*/ ms_setIFrameHeight_stopper = false;
function ms_setIFrameHeight(/*String*/flyoutId) {
	// Only IE6
	if (!window.attachEvent || window.opera || window.XMLHttpRequest) return;
	// Get IFrame and FlyOut element
	if (flyoutId == '') return;
	// Only CORE NAV Flyout Type 2
	
	var flyout = document.getElementById(flyoutId);
	var /*XMLNode*/ iframe = flyout.getElementsByTagName("iframe")[0];
	if (!iframe) return;
	var /*XMLNode*/ list = flyout.getElementsByTagName("ul")[0];
	if (!list) return;
	var /*NodeList*/ lists = flyout.getElementsByTagName("ul");
	if (!lists) return;
	var /*int*/ height = 0;
	for (var i = 0; i < lists.length; i++) {
		height += lists.item(i).offsetHeight;
	}
			
	flyout.style.display = "";
	if (!ms_setIFrameHeight_stopper) {
		// Sometimes IE needs some time to render the hidden
		// elements, so we give it 25 additional milliseconds.
		ms_setIFrameHeight_stopper = true;
		setTimeout("ms_setIFrameHeight('" + flyoutId + "')", 25);
		return;
	} else if (ms_setIFrameHeight_stopper) {
		ms_setIFrameHeight_stopper = false;
	}
	
	// Set height on IFrame. In some cases the height of the iframe must be
	// larger than that of the list because the whole flyout is larger.
	if(iframe.parentNode.className == 'flyContent2'){
		iframe.style.height = (height + 18) + "px";
	}
	else if(iframe.parentNode.className == 'flyWrap'){
		iframe.style.height = 390 + "px";
	}
	
	else{
		iframe.style.height = height + "px";
	}
}



// Methods
Flyout.prototype.activate = function() {
	// Clear Closing Timeout
	clearTimeout(Flyout.changeTimeout[this.type]);
	clearTimeout(Flyout.closeTimeout[this.type]);

	// Closing all independend, open menus
	for (var type in Flyout.openFlyout) {
		var /*boolean*/ isIndependend = true;
		
		isIndependend = ((type != Flyout.TYPE_CORENAV1) && (type != Flyout.TYPE_CORENAV2));
			
		// If current flyout type is independend and has open flyouts, close them
		if (isIndependend && Flyout.openFlyout[type]) {
			Flyout.closeAll(type);
		}
	}

	if (Flyout.openFlyout[this.type] == undefined) {
		// If no Flyout Menu is open, strictly open the current one
		this.handleActivate();
	} else if (Flyout.openFlyout[this.type] != this) {
		Flyout.changeTimeout[this.type] = setTimeout("Flyout.change(\"" + this.id + "\", \"" + this.type + "\")", Flyout.ACTIVATION_TIMEOUT[this.type]);
	}
}

Flyout.prototype.handleActivate = function() {
	// Call Ajax Request for Core Navigation 2 Flyout
	if (this.type == Flyout.TYPE_CORENAV2) {
		var /*String[]*/ values = this.id.split("@");
		var /*String*/ elementId = values[0];
		var /*String*/ handle = values[1];
		
		//ms_corenav_loadFlyoutData(elementId, handle);
		ms_setIFrameHeight(this.flyout.id);
	} else {
		ms_setIFrameHeight(this.flyout.id);
	}
	
	// Activate Flyout
	this.active = true;
	Flyout.pushClass(this.flyout, Flyout.CSS_CLASS_HOVER);
	
	// Set openFlyout Reference
	Flyout.openFlyout[this.type] = this;
	
}

function getIEVersion() {
	try {
		if (navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion.indexOf("MSIE")>0) {		
			var str = navigator.appVersion;
			var ind = parseInt(str.indexOf("MSIE"))+5;
			var ver = parseInt(str.substring(ind, ind+1));
			return ver;
		} else {
			return 0;
		}
	} catch(e) {}
}

Flyout.prototype.deactivate = function() {
	// Set Closing Timeout
	Flyout.closeTimeout[this.type] = setTimeout("Flyout.closeAll(\"" + this.type + "\")", Flyout.DEACTIVATION_TIMEOUT[this.type]);
}



getMenuHeight=function(menu) {

	var h = $('.flyWrap-' + menu).outerHeight();

	if(getIEVersion() == 6){
		$('.flyContent-' + menu).css({'height': h});
		
	}
	else{
		
	 	$('.flyContent-' + menu).css({'min-height' : h});
		
	}

}



