/** Function that hides and shows divs for advanced and simple search on fi1
 * 
 * @name Fi1_showHide
 * @param objShow - div's id that needs to be shown
 * @param objHide - div's id that needs to be hidden
 */
function Fi1_showHide(objShow, objHide){
	jQuery(objShow).show();
	jQuery(objHide).hide();
	if(objShow == '#hide'){
		jQuery('select.disable').attr("disabled", false);
	}else{
		jQuery('select.disable').attr("disabled", true);
	}
}
/**
 *	Function that inits the car's pictures, resize them and calculates the rest of the elements based on number of pictures loaded
 *	
 *	@name Fi5_initPhotoList
 * 	@param -
 *	@return -
 *
 */
function Fi5_initPhotoList(){
	var max_width=50;
	var max_height=33;									
	
	$(window).load(function(){									

		$("div.thumbnails").find(".photoList > a").each(function(){		
			var width = $(this).find("img").width();
			var height = $(this).find("img").height();
			var src = $(this).find("img").attr("src");				
			
			arrPhotoList[intTotalPhoto++]=$(this).attr("name");

			if (width > max_width) {					
				var ratio = (height / width );
				width = max_width;
				height = (width * ratio);
			}  
			if(height > max_height) {	
				var ratio = (height / width);
				height = max_height;
				width = (height / ratio);
			} 
								
			$(this).find("img").height(height).width(width);	
			$(this).find("span").width(width).height(height);
			
			if(src == srcLastPath){
				$(this).find("span").css("display","none");					
			}										
							
			$(this).find("div.smallBlock").html(intTotalPhoto);
		});
		
		$("#indexDetails").html(1+"/"+intTotalPhoto);
		
		if(intTotalPhoto==1){				
			$("#prevButton").html($("#prevButton").text());
			$("#linkEnabled").css("display","none");
			$("#linkDisabled").css("display","");
		} else if (intTotalPhoto>1) {
			$("#prevButton").html($("#prevButton").text());
			$("#linkEnabled").css("display","");
			$("#linkDisabled").css("display","none");
		} else {
			$("div.thumbnails").css("display","none");				
		}
		$(".thumbnails").css("visibility", "visible");
	});	
}

/**
 *	Function that verify the picture position in the list and activate/deactivate navigation links
 *	
 *	@name Fi5_indexCheck
 * 	@param	int intIndex (The current picture position in the list)
 *	@param	int intTotalPhoto (The total number of pictures in the list)
 *	@return -
 *
 */
function Fi5_indexCheck(intIndex,intTotalPhoto){
	if(intIndex==(intTotalPhoto-1)){						
		$("#linkEnabled").css("display","none");
		$("#linkDisabled").css("display","");
	}
	if(intIndex>0){
		$("#prevButton").html("<a href='#photoBig'>"+$("#prevButton").text()+"</a>");
	}
	if(intIndex<(intTotalPhoto-1)){						
		$("#linkEnabled").css("display","");
		$("#linkDisabled").css("display","none");
	} 
	if(intIndex==0){
		$("#prevButton").html($("#prevButton").text());
	}
	intIndex++;
	$("#indexDetails").html(intIndex+"/"+intTotalPhoto);
}

/**
 *	Function that controls the actions of an image on mouse events
 *	
 *	@name Fi5_photoListActions		 
 *	@param	-
 *	@return -
 *
 */
function Fi5_photoListActions(){
	$("div.thumbnails").find(".photoList > a").each(function(){				
		var src=$(this).find("img").attr("src");
		$(this).mouseover(function(){
			$(this).find("span").css("display","none");					
		})
		.mouseout(function(){
			if(src != srcLastPath){
				$(this).find("span").css("display","");
			}
		})				
		.click(function(){
			var intIndex = Fi5_in_array($(this).attr("name"), arrPhotoList);
			Fi5_indexCheck(intIndex,intTotalPhoto);
			
			$("#photoBoxCRS1").attr("src",src);					
			$("div.thumbnails").find(".photoList > a[name='"+srcLastSelector+"']").find("span").css("display","");
			
			srcLastPath = src;
			srcLastSelector = $(this).attr("name");
			
		});
	});
}

/**
 *	Function that controls the actions of the link next picture on mouse click
 *	
 *	@name Fi5_nextButtonActions		 
 *	@param	-
 *	@return -
 *
 */
function Fi5_nextButtonActions(){
	$("#nextButton").click(function(){
		var intIndex = Fi5_in_array($("div.thumbnails").find(".photoList > a[name='"+srcLastSelector+"']").attr("name"), arrPhotoList);
						
		if((intIndex>-1)&&(intIndex<intTotalPhoto-1)){
			intIndex++;
			var src=$("div.thumbnails").find(".photoList > a[name='"+arrPhotoList[intIndex]+"']").find("img").attr("src");
			$("#photoBoxCRS1").attr("src",src);
			$("div.thumbnails").find(".photoList > a[name='"+srcLastSelector+"']").find("span").css("display","");
			$("div.thumbnails").find(".photoList > a[name='"+arrPhotoList[intIndex]+"']").find("span").css("display","none");					
			
			srcLastPath = src;
			srcLastSelector = arrPhotoList[intIndex];
		}
						
		Fi5_indexCheck(intIndex,intTotalPhoto);
	});
}

/**
 *	Function that controls the actions of the link previous picture on mouse click
 *	
 *	@name Fi5_prevButtonActions		 
 *	@param	-
 *	@return -
 *
 */
function Fi5_prevButtonActions(){
	$("#prevButton").click(function(){
		var intIndex = Fi5_in_array($("div.thumbnails").find(".photoList > a[name='"+srcLastSelector+"']").attr("name"), arrPhotoList);
						
		if((intIndex>-1)&&(intIndex>0)){
			intIndex--;
			var src=$("div.thumbnails").find(".photoList > a[name='"+arrPhotoList[intIndex]+"']").find("img").attr("src");
			$("#photoBoxCRS1").attr("src",src);
			$("div.thumbnails").find(".photoList > a[name='"+srcLastSelector+"']").find("span").css("display","");
			$("div.thumbnails").find(".photoList > a[name='"+arrPhotoList[intIndex]+"']").find("span").css("display","none");					
			
			srcLastPath = src;
			srcLastSelector = arrPhotoList[intIndex];
		}
						
		Fi5_indexCheck(intIndex,intTotalPhoto);
	});
}

/**
 *	Checks if a value exists in an array and returns the index
 *	
 *	@name Fi5_in_array		 
 *	@param	string strCheck (The string that I look)
 *	@param	array arrArray (The array where I look)
 *	@return int
 *
 */
function Fi5_in_array(strCheck, arrArray) {
	for(var intJ in arrArray) {
		if (arrArray[intJ] == strCheck){
			return intJ;
		}
	}
	return -1;
}

/**
 * Submit the selected cars
 *
 * @name 	Fi9_submitSelectedCars
 * @param	string strFormName (The name of the form which contains the checkboxes)
 * @param	int intMaxNumberCars (The max number of cars that should be retained for comparison) 
 * @param	string strLinkRedirect (The link for redirection)
 * @returns	-
 */
function Fi9_submitSelectedCars(strFormName, intMaxNumberCars, strLinkRedirect){
	var objForm=document.getElementById(strFormName);
	var strCarCompareList="";						
	var intCurrNumberCars=0;
	
	for (var i=0;i<objForm.length;i++){
		if(objForm.elements[i].type=="checkbox"){
			if(objForm.elements[i].checked){
				intCurrNumberCars++;
				if(intCurrNumberCars>intMaxNumberCars){
					break;
				}
				strCarCompareList+=objForm.elements[i].value+",";
			}
		}
	}
	if(strCarCompareList.length){
		strLinkRedirect+=strCarCompareList.substr(0, strCarCompareList.length-1);
		window.location=strLinkRedirect;
	}			
}

/**
 * Change the value of an hidden field than submit the form
 *
 * @name 	Fi9_submitForm
 * @param	string strFormName (The name of the form which contains the hidden fields)		 
 * @param	string strHiddenName (The name of the hidden field)
 * @param	int intValue (The value of the hidden field that I want to set)
 * @returns	-
 */
function Fi9_submitForm(strFormName, strHiddenName, intValue){
	var objForm=document.getElementById(strFormName);
	var objHidden=document.getElementById(strHiddenName);			
	
	objHidden.value=intValue;			
	objForm.submit();
}

/**
 * Checks how many cars are selected; if equals intMaxNumberCars then we are disabling the rest of the checkboxes; else we are enabling the rest of the checkboxes
 *
 * @name 	Fi9_checkSelected
 * @param	string strFormName (The name of the form which contains the checkboxes)
 * @param	int intMaxNumberCars (The max number of cars that should be retained for comparison) 		 
 * @returns	-
 */
function Fi9_checkSelected(strFormName, intMaxNumberCars){
	var objForm=document.getElementById(strFormName);			
	var intCurrNumberCars=0;
	
	for (var i=0;i<objForm.length;i++){
		if(objForm.elements[i].type=="checkbox"){
			if(objForm.elements[i].checked){
				intCurrNumberCars++;						
				if(intCurrNumberCars==intMaxNumberCars){
					break;
				}
			}
		}
	}
	
	if(intCurrNumberCars==intMaxNumberCars){
		for (var i=0;i<objForm.length;i++){
			if(objForm.elements[i].type=="checkbox"){
				if(!objForm.elements[i].checked){
					objForm.elements[i].disabled=true;
				}
			}
		}
	}else{
		for (var i=0;i<objForm.length;i++){
			if(objForm.elements[i].type=="checkbox"){	
				if(objForm.elements[i].disabled){
					objForm.elements[i].disabled=false;						
				}
			}
		}
	}
}

/**
 *	Function that controls the actions of "Vis detaljer" link
 *	
 *	@name Fi10_plusActions		 
 *	@param	-
 *	@return -
 *
 */
function Fi10_plusActions(){
	$("#plus").click(function(){
		$(".smallDiv").css("height","auto");
		$("#minus").css("display","");
		$("#plus").css("display","none");
	});
}

/**
 *	Function that controls the actions of "Skjul detaljer" link
 *	
 *	@name Fi10_minusActions		 
 *	@param	-
 *	@return -
 *
 */
function Fi10_minusActions(){
	$("#minus").click(function(){
		$(".smallDiv").css("height","15px");
		$("#minus").css("display","none");
		$("#plus").css("display","");
	});
}
	
/**
 *	Function that resets the form
 *	
 *	@name Fi11_resetForm
 * 	@param string strFormName (The name of the form)
 *	@return boolean
 *
 */
function Fi11_resetForm(strFormName){
	var objForm=document.getElementById(strFormName);									
	
	for (var i=0;i<objForm.length;i++){
		if(objForm.elements[i].type=="select-one"){
			objForm.elements[i].value = 0;					
		} else {
			objForm.elements[i].value = "";
		}
	}
	
	return true;
}