// JavaScript Document
$(document).ready(function() {

    //Filter Country, State and City fields
    $.setupSublist("country");
	
	// Add first-child and last-child classes for ie6+ie7
	$("li:first-child").addClass("first-child");
	$("li:last-child").addClass("last-child");
	$("#footer ul li:last-child").addClass("last-child");
	$("#footer .legal ul li:first-child").addClass("first-child");
	$(".product-detail:last-child").addClass("last-child");
	$(".product-category .row:last-child").addClass("last-child");
	$("#body .product-info .info li:first-child").addClass("first-child");
	$("#body .product-info .info li:last-child").addClass("last-child");
	$("#body.container .more-rental .inner .item:last-child").addClass("last-child");
	$("#body.container .news-listing:last-child").addClass("last-child");
	$("#body.container .position-vacant .details:last-child").addClass("last-child");
	$(".blog-listing .info li:last-child").addClass("last-child");
	$(".blog-listing .info li:first-child").addClass("first-child");

	
    $('input[class="textbox"]').focus(function() {
    $(this).addClass("focus");
});
    $('input[class="textbox"]').blur(function() {
    $(this).removeClass("focus");
});
	
	
	//Font Resize //
	
	$('#page-tools .font-increase').click(function(){
		 $("body").css("font-size","90%");
		return false;
	});

	$('#page-tools .font-decrease').click(function(){
		 $("body").css("font-size","80%");
		return false;
	});
	
	//Print //
	
	$(".print a").click(function() {
		var popurl;
		if (location.href.indexOf("?") != -1) {
			popurl = location.href+"&print=true";
		} else {
			popurl = location.href+"?print=true";		
		}
		window.open(popurl,"Print","toolbar=1,scrollbars=1,menubar=1,width=750,height=520");
		return false;
	});


	if (location.href.indexOf("print=true") != -1) {
		$("link").each(function() {
			if ($(this).attr("media") == "print") {
				if ($.browser.msie) { // BrowserDetect.browser == "Explorer") {
					$(this).attr("media","all");
				} else {
					$(this).replaceWith('<link rel="stylesheet" type="text/css" href="/includes/styles/print.css">');
				}
			} else {
				$(this).remove();
			}
		}); 
		
		//print dialog box
		$("a").attr("href","#");
		$("body").append('<script type="text/javascript">print();</script>');
	}

//	$('#productnav li').append('<div class="hover"><\/div>');


	//$('#productnav li').hover(
		//Mouseover, fadeIn the hidden hover class	
//		function() {
			
//			$(this).children('div').fadeIn('1000');	
//		}, 
		//Mouseout, fadeOut the hover class
//		function() {
		
//			$(this).children('div').fadeOut('1000');
				
//	}).click (function () {
	
		//Add selected class if user clicked on it
//		$(this).addClass('selected');
		
//	});
	
	//FAQ
	
	//Hide (Collapse) the toggle containers on load
	$(".faq-container").hide(); 

	//Switch the "Open" and "Close" state per click
	$("h4.faq-title").toggle(function(){
		$(this).addClass("active");
		}, function () {
		$(this).removeClass("active");
	});

	//Slide up and down on click
	$("h4.faq-title").click(function(){
		$(this).next(".faq-container").slideToggle("slow");
	});

	
		// Glossary
	$(".glossary h2").hide();
	$('.glossary dl').hide();

	$('.glossary').prepend('<div class="navigation"><ul></ul></div>')
	var numLetters = $('.glossary h2').length;
	$('.glossary h2').each(function(i){
		var title = $(this).text();
		var link;
		if ($('.glossary #'+title).length > 0) {	
			link = '<li><a href="#' + title + '">' + title + '<span class="bottom"></span></a></li>';
		} else {
			link = '<li><span class="noitems">' + title + '</span></li>';
		}			
		$('.glossary .navigation ul').append(link);
	});
	
	$('.glossary .navigation a').click(function(){
		$('.glossary .navigation ul li').removeClass("selected");
		$(this).parent("li").addClass("selected");
		var id = '#'+this.href.split('#')[1];
		$('.glossary dl').hide();
		$(id).show();
		return false;
	});
	
	$(".glossary dl:first").show();
	$(".glossary .navigation li:first").addClass("selected");

	
	//Search Box text

    $(".query").focus(function(srcc)
    {
        if ($(this).val() == $(this)[0].title)
        {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });
    
    $(".query").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });
    
    $(".query").blur();        
});

jQuery.setupSublist = function(keyword) {
 
	var $parent = $("select[rel="+keyword+"-parent]");
	var $child = $("select[rel="+keyword+"-child]");
	var $parentContainer = $parent.parent();
	var $childContainer = $child.parent();	
	var $linkedFields = $("input[rel|="+keyword+"]");
 
	var activeSelection = $parent.find("option:selected").attr("value");
 
	// Hide linked fields on initialisation
	$linkedFields.parent().hide();
	
	// Create a clone of the child select box
	var $childClone = $child.clone().appendTo($childContainer);
	$childClone.removeAttr("name").removeAttr("id").removeAttr("rel");
	$childClone.hide();
	
	// Initialise options
	updateChildOptions(activeSelection);
	updateLinkedFields(activeSelection);	
	
	// Bind change function
	$parent.change(function(){
		var filter = $(this).find("option:selected").attr("value");
		
		// Update child options and linked fields if necessary
		if(filter != activeSelection) {
			updateChildOptions(filter);
			updateLinkedFields(filter);
		}		
	});
	
	function updateChildOptions(filter){
		
		// Empty the select box
		$child.empty();
		$child.attr("disabled","disabled");				
		$child.append('<option value="null" selected="selected">-- Please select --</option>');
 
		// Populate with matching options
		$childClone.find("optgroup[label='"+filter+"'] option").each(function(index){
			if(index==0){
				$child.removeAttr("disabled");		
			}
			$child.append($(this).clone());
			activeSelection = filter;
		});
		
	}
	
	function updateLinkedFields(filter){
 
		// Hide all linked fields
		$linkedFields.parent().hide();
 
		// Show any matching linked fields
		$linkedFields.each(function(){
 
			// Remove spaces and convert to lowercase
			var simpleFilter = filter.split(' ').join('').toLowerCase();
			
			// Show matching options
			if($(this).attr("rel") == keyword+"-"+simpleFilter){
				$(this).parent().show();
			}
		});
	}
	
 
};