$(document).ready(function() {

	//Higlight Alert box, row 1 column 1, on homepage
/*	try {
		var appendAlert = '<div id="append" style="height:196px;width:308px;background-color:yellow;position:absolute; top:0;left:0;border-radius:8px;-moz-border-radius:8px;-webkit-border-radius:8px;"></div>'
		$("#Row1Col1").css('position','relative').append(appendAlert);
		$("#append").hide().delay('1000').fadeTo('fast', 0.5).delay('50').fadeTo('fast', 0.1).delay('50').fadeTo('fast', 0.5).delay('50').fadeOut('slow');
	}
	catch(err) {
	}*/

	try
	{
		var msie6 = $.browser == 'msie' && $.browser.version < 7;

		if (!msie6) {
			var top = $('.bookingrightcol_wrap').offset().top - parseFloat($('.bookingrightcol_wrap').css('margin-top').replace(/auto/, 0));
			$(window).scroll(function (event) {
				// what the y position of the scroll is
				var y = $(this).scrollTop();

				// whether that's below the form
				if (y >= top) {
					// if so, ad the fixed class
					$('.bookingrightcol_wrap').addClass('fixed');
				} else {
					// otherwise remove it
					$('.bookingrightcol_wrap').removeClass('fixed');
				}
			});
		}
	}
	catch(err)
	{
	}

	// NEW Validation technique (wish me luck!)
	$(".validate").click ( function() { // intercept form submission if 'validate' class given to button
		var valid; // Set valid variable to null
		$(".validateField").each(function() { // Find each field set to validate
			var container = $(this); // Current field to validate
			var inputType = $(this).find("input").attr('type'); // Type of input within the current field
			
			$(this).find("input").each(function() { // Starts function for current input
				switch(inputType) {
				    case 'checkbox':
						var checked = $(this).is(":checked"); // ture/false variable
						if(checked) {
							// If valid not already set to false, set it to true
							if(valid == false ? valid = false : valid = true);
						}
						else {
							container
								.css('position', 'relative')   // allow positioning child div relative to parent
							
							// create and append .background div 
							// initially visible (highlight), fading with sub-sequent animate
								.append( $("<div>")
									.attr('class', 'background')
									.css({
										width:container.width(), 
										height:container.height()
									})
								);
							$(".background").animate({opacity:0}, 2500); //fade .background to normal
							$(this)[0].focus(); // focus problem field
							valid = false;
						}
						break;
					case 'text':
						var text = $(this).text(); // Field text
						if(text !== "") {
							if(valid == false ? valid = false : valid = true);
						}
						else {
							container
								.css('position', 'relative') 
								.append( $("<div>")
									.attr('class', 'background')
									.css({width:container.width(), height:container.height()})
								);
							$(".background").animate({opacity:0}, 2500); //fade .background to normal
							$(this)[0].focus(); // focus problem field
							valid = false;
						}
						break;
					default:
				}
			});
		});
		if(valid) {
			return true;
		}
		else {
			return false;
		}
	});
	
	function validateHighlight() {
		return;
	}
	
	// FAQs	
	//var faqObj = new Type.registerNamespace("CIM.SmartInterfaceV2.ContensisSiteData.SiteData");
	var delay = 500;
	var $faqObj = new Object("CIM.SmartInterfaceV2.ContensisSiteData.SiteData");
	$(".faqToggleLink")
		.hover(function () { // on mouse over
			$(this).stop().animate({opacity: 1}, 'fast'); // animate opacity to full
		}, function () { // on mouse out
			$(this).stop().animate({opacity: 0.5}, 'slow'); // animate opacity to nill
		})
		.click( function() {
			var $rel = $(this).parent().parent().attr("rel");
			
			$(".faqAnswer").each( function() {
				
				var $id = $(this).attr("id");
				
				if ($id !== $rel) {
					$(this).stop().slideUp(delay).removeClass('active');
				}
				else {
					if($(this).is('.active')) {
						$(this).stop().slideUp(delay).removeClass('active');
					}
					else {
						$(this).stop().slideDown(delay).addClass('active');
						//faqHit("Faq_GetFaqAnswerById", $id, AjaxSucceeded, AjaxFailed);
						
					}
				} 
			});
		});
		function faqHit(fn, $faqId, successFn, errorFn) {
						$.ajax({
							type: "POST",
							contentType: "application/json; charset=utf-8",
							url: $faqObj + fn,
							data: "{'faqId':$faqId}",
							dataType: "json",
							success: successFn,
							error: errorFn,
							timeout: 3000
						});
		}
		function AjaxSucceeded (response)  
		{   
			alert(response);  
		}
		function AjaxFailed (response)  
		{   
			alert("Our systems are currently not responding: " + response);
		}


	

	// My CIM Admin page menu hide function
	$(".menuOn").animate({opacity: 0.2}, 'slow'); //initial set opacity on pageload
	
	$(".menuOn").hover(function () { // on mouse over
		$(this).stop().animate({opacity: 1}, 'slow'); // animate opacity to full
	}, function () { // on mouse out
		$(this).stop().animate({opacity: 0.2}, 'slow'); // animate opacity to nill
	});
		
	$("#menuOff").click( function() { 
		$(".sys_leftcolumn").addClass("hideMenu"); // hides menu
		$(".sys_maincontent").addClass("expandContent"); // expands <div class="sys_maincontent">
		$(".sys_maincontentareawrapper").addClass("noBG"); // hides leftnav background
		$(".sys_headertopnavselectedbar").attr("id","item0"); // changes top background to full width
		$(".sys_footer").attr("class","sys_footer_home"); // changes footer background to full width
		$("#menuOn").show(); // shows menuShow button
		$(this).hide(); // hides menuHide button
	});
	$("#menuOn").click( function() {
		$(".sys_leftcolumn").removeClass("hideMenu"); // shows menu
		$(".sys_maincontent").removeClass("expandContent"); // contracts <div class="sys_maincontent">
		$(".sys_maincontentareawrapper").removeClass("noBG"); // shows leftnav background
		$(".sys_headertopnavselectedbar").attr("id","item00"); // changes top background to 2 column
		$(".sys_footer_home").attr("class","sys_footer"); // changes footer background to 2 column
		$("#menuOff").show(); // shows menuHide button
		$(this).hide(); // hides menuShow button
	});

	
	// Site-wide search button activate
	$("#F1_formObject3_Finish").click( function() {		
		var textFilled = $("#F1_formObject2").val();
		if(textFilled == "") {
			return false;
		}
		else{
			$(this).css("background","transparent url('/images/layout/bkgrnd_search_loading.gif') no-repeat scroll center 4px");
			return true;
		}
/*		$("#F1_formObject2").val("Search disabled.").css('font-style','italic').attr('readonly','readonly');
		return false;*/
	});
	
	
	
	
/*	$(".sys_16 a").each(function() {
		$(this).attr("target","_blank");
		$(this).attr("title","Opens in a new window");
	});*/
	
	$(".sys_headertopnav a,.sys_menu a").each(function() {
		$(this).removeAttr("title");
	});
	
	
	// Button disable script (mainly Join)
	/*$(".clickAction").click(function () {
			$(this).toggle();
			$(".actionText").toggle();
	});*/

	
	
	// Shopping Cart Animation Scrpts	
	$("#okError").click(function () {
		$("#validator").fadeOut();
		$("#AddToCart_txtAddQuantityID").val("1");
	});
	
	$(".button").click(function() {
		var button = $(this)
		button.attr("value","wait...");
	});
	
	// CIM HEADER CHANGER
	var domain = window.location.hostname //jQuery.url.attr("host");
	if (domain == "livebeta.cim.co.uk")
	{
		$(".sys_header").attr("id","livebeta");
	}
	else if (domain == "preview.cim.co.uk")
	{
		$(".sys_header").attr("id","previewcimv2");
	}

	SetupTooltips();
});

ShowTooltip = function(e)
{
	var text = $(this).next('.show-tooltip-text');
	if (text.attr('class') != 'show-tooltip-text')
		return false;

	text.show()
		.css('top', e.pageY)
		.css('left', e.pageX+10);

	return false;
}
HideTooltip = function(e)
{
	var text = $(this).next('.show-tooltip-text');
	if (text.attr('class') != 'show-tooltip-text')
		return false;

	text.hide();
}

SetupTooltips = function()
{
	$('.show-tooltip')
		.each(function(){
			$(this)
				.after($('<span/>')
					.attr('class', 'show-tooltip-text')
					.html($(this).attr('title')))
				.attr('title', '');
		})
		.hover(ShowTooltip, HideTooltip);
}


		// EVENT CALENDAR POP-UPS
	$(function()
	{
		try {
			$('.dateInput').datePicker();
			$('.startDate').bind(
				'dpClosed',
				function(e, selectedDates)
				{
					var d = selectedDates[0];
					if (d) {
						d = new Date(d);
						$('.endDate').dpSetStartDate(d.addDays(1).asString());
					}
				}
			);
			$('.endDate').bind(
				'dpClosed',
				function(e, selectedDates)
				{
					var d = selectedDates[0];
					if (d) {
						d = new Date(d);
						$('.startDate').dpSetEndDate(d.addDays(-1).asString());
					}
				}
			);
		}
		catch(err) {
		}
	});
