$(document).ready(function(){
	
	$(".iframe").colorbox({width:"600px", height:"620px", iframe:true});
	
});


$(function(){
	// ad js css class to body & get the id of the body
	var myBody = $("body");
	myBody.addClass("js");
	var pid = myBody.attr("id");
	
	//jquery
	initForm(pid);
	initFormSubmit();
	initAutocomplete(pid);
	initChangePropulsion(pid);
	$("#language_dropdown").isDropDownMenu();
	$("#login_dropdown").isDropDownMenu();
	countryChange(pid);
	initValidation(pid);
	initColorBox(pid);
	initSlideShow(pid);
	
});

function initSlideShow(pid){
	if(pid=="index"){
		$('#hs-show').cycle({
			next: '#hs-next',
			prev: '#hs-prev',
			timeout: 5000,
			pause : true,
			sync: true
		});
	}
}

function initColorBox(pid){
	if(pid == 'viewboat'){
		$("a[rel='group1']","#PageBody").colorbox({slideshow:true});		
	}
}

function initValidation(pid){
	if(pid == 'sendurlto'){
		$("#form_dosendurlto").validate({
			rules: {
				addr: {
					required: true,
					email: true
				}
			}
		});
	}else if(pid == 'login'){
		$("#form_login").validate({
			rules: {
				Boatshed_Login_Email: {
					required: true,
					email: true
				}
			}
		});
	}
}

function initChangePropulsion(pid){
	if (pid == "boatfinder") {
		var me = $('#BoatFinder_Propulsion');

		me.change(function(){
			getPropulsion($(this).val());
		});
		
		function getPropulsion(propulsion){
			$.ajax({
				type: "GET",
				url: "_hull_configurations.php?propulsion=" + propulsion + "&grouped=true",
				dataType: "json",
				success: function(json){
					var output = '';
			 		for(i = 0; i < $(json).length; i++){
						output += '<option value="'+$(json)[i]['key']+'">'+$(json)[i]['value']+'</option>';
			 		}		
					$("#BoatFinder_Hull_Configuration").html(output);
				}
			});
		}
		
		getPropulsion(me.val());
	}
}

function initFormSubmit(){
	$(".inlinesubmit").click(function(){
		$(this).parents("form").submit();
		
		return false;
	});
}

$.fn.isDropDownMenu=function(){
	var A=$(this);
	
	A.prev().click(function(){
		if(!$(this).hasClass("Open")) $(".Open").removeClass("Open").next().hide();

		$(this).toggleClass("Open").next().toggle();

		return false;
	});

	A.mouseup(function(){
		return false;
	});
	
	A.prev().mouseup(function(){
		return false;
	});

	$(document).mouseup(function(B){
		if(A.prev().hasClass("Open")){
			A.prev().removeClass("Open").next().hide();
		}
	});
};

function initForm(pid){
	if(pid == "listmyboat" || pid == "search" || pid == "register" || pid == "boatfinder"){
		$("input, select, textarea","fieldset").focus(function(){
			$(this).parent().addClass("Focus");
		});
		$("fieldset input,fieldset select,fieldset textarea").blur(function(){
			$(this).parent().removeClass("Focus");
		});
	}
	
	var btiv = $('#form_boattype').val();
	var loaiv = $('#form_length').val();
	var apiv = $('#form_price').val();
	var refiv = $('#form_boatref').val();
	
	$("#SearchForm input[type='text']").each(function(){
		var initVal = $(this).val();
		var field = $(this);
		
		field.focus(function(){
			if(initVal == field.val()) field.val('');
		});
		field.blur(function(){
			if(field.val() == '') field.val(initVal);
		});
	});
	
	$("#SearchForm").submit(function(){
		var bl = $('#form_length').val();
		if(bl == loaiv) $('#form_length').val('');
		
		var bp = $('#form_price').val();
		if(bp == apiv) $('#form_price').val('');
		
		var br = $('#form_boatref').val();
		if(br == refiv) $('#form_boatref').val('');
		
		var bt = $('#form_boattype').val();
		if(bt == btiv) $('#form_boattype').val('');
	});
		
	if(pid != "search"){
		var aln = $('#AdvancedSearch').html();
		var bln = $('#AdvancedSearch').attr('class');
		
		$('#AdvancedSearch').click(function(){
			$('#AdvancedFieldset').toggle();
			
			var BT = ($('#AdvancedFieldset').is(':visible')) ? bln : aln; 
			$(this).html(BT);
			
			return false;
		});
	}
}

function countryChange(pid){
	if(pid == "search" || pid == "index" || pid == "dosearch" || pid == "forsale"){
		$("#form_country").change(function(){
			var me = $(this).val().split("2");
			
			if(me[0] == 'US' || me[0] == 'CA' ){
				$("#form_state").removeAttr("disabled");
			}else{
				$("#form_state").attr("disabled","disabled");
			}
		});
	}
}

function initAutocomplete(pid){
	if(pid == 'boatfinder'){
		var mc = 2;
		var bw = 260;
		var mr = 30;
		var sf = false;
		
		$("#BoatFinder_Manufacturer").autocomplete(
			"suggest_typemodelmanu.php",{
				extraParams: {'type': 'manufacturer'},
				width: bw,
				selectFirst: sf,
				minChars: mc,
				max: mr
		});
		
		$("#BoatFinder_Model").autocomplete(
			"suggest_typemodelmanu.php",{
				extraParams:{'type':'model'},
				width: bw,
				selectFirst: sf,
				minChars: mc,
				max: mr
		});
	}
}

//old

// Used in 'listmyboat.php'
/*if(pid == 'listmyboat'){
	function getvalue(field_name) {
		if (document.forms["0"].elements[field_name] == null){
			return "-";
		} 
		return document.forms["0"].elements[field_name].value;
	}
			
	function checkform() {
		valid = true;
		emailokay = true;
		
		if (getvalue("form_name") == "") valid = false;
		if (getvalue("form_phone") == "") valid = false;
		if (getvalue("form_email") == "") valid = false;
		if (getvalue("form_make") == "") valid = false;
		if (getvalue("form_year") == "") valid = false;
		if (getvalue("form_location") == "") valid = false;
		
		if (!valid){
		 	txt = "Please complete all mandatory fields!";
		    if( emailokay == false ){
		    	txt = txt + "\nEnsure your email address is typed correctly.";
			}
		    alert(txt);
		}
		return valid;
	}
}*/

//boat finder js

	/*function getvalue(field_name) {
		if (document.forms["0"].elements[field_name] == null) return "-";
		return document.forms["0"].elements[field_name].value;
	}
	
	function allDigits(str){
		return inValidCharSet(str,"0123456789.,");
	}
	
	function inValidCharSet(str,charset){
		var result = true;
		// Note: doesn't use regular expressions to avoid early Mac browser bugs
		for (var i=0;i<str.length;i++)
			if (charset.indexOf(str.substr(i,1))<0){
				result = false;
				break;
		}
		return result;
	}
	
	function checkform() {
		valid = true;
		if (getvalue("form_price") == "") valid = false;
		if (!allDigits(getvalue("form_price"))) valid = false;
		if (!valid) alert("Please enter an amount for budget!");
		return valid;
	}*/
	
function openpicture(n,i) {
	temp = window.open("./_biglinkimage.php?newsitem=" + n + "&imageno=" + i, "_newsimg", "width=420,height=400,resizable=no,scrollbars=no");
	return;
}