$(document).ready(function() {

    $('#search_keywords').keydown(function(ev) {
			if(ev.keyCode==13) {
				$(this).parents('form').submit();
				return false;
			}
		});
    
    // Menu Popup function
    $(".menu-popup").hide();
	$("#main-nav ul.mnu li").hover(function() {
		var pos = $(this).position();
        $("div.content-panels").css("z-index","-1");
        $("#google-map").css("z-index","-1");
        $(".box8").css("z-index","-1");
		$(this).children("div").css("left",(pos.left-6)+"px").css("top",(pos.top+18)+"px").css("z-index","1").show();
    }, function(evt) {   
        $("div.content-panels").css("z-index","0");
        $("#google-map").css("z-index","0");
        $(".box8").css("z-index","0");
        $(".menu-popup").hide();
    });
    
    // rollover effect for homepage thumbnail wall
    $(".wall li[class!=large] img[class!=on]").css("opacity","0.6");
    $(".wall li[class!=large] img").hover(function() {
        $(this).css("opacity","1");
    }, function() {
        var id = $(this).parent().attr("id");
        if ($("#"+id+"panel").hasClass("on")) {
            
        } else {
            $(this).css("opacity","0.6");
        }
        
    })   
    
    // collapse footer / form
    $("#footer-collapse").bind("click", function() {
       if ($("#footer2").css("display") == 'none') {
            $("#footer2").slideDown();
            $(this).attr("src","/images/colapse1.gif");
       } else {
            $("#footer2").slideUp();
            $(this).attr("src","/images/expand1.gif");
       }
       return false; 
    });
    $("#collapse-enquiry").bind("click", function() {
       if ($("#enquiryform").css("display") == 'none') {
            $("#enquiryform").slideDown();
            $(this).attr("src","/images/colapse1.gif");
       } else {
            $("#enquiryform").slideUp();
            $(this).attr("src","/images/expand1.gif");
       }
       return false; 
    });
    
    // Accordion function
	$(".accordion li h4").bind("click", function() {
		$(this).parent().parent().children("li").children("div").slideUp();
		if ($(this).parent().children("div").css("display") == 'none') {
			$(this).parent().children("div").slideToggle();
		} else {
			$(this).parent().children("div").slideUp();
		}
	});
    
    // wall thumb clicks
    $(".clickarea").bind("mouseover", function() {
       var id = $(this).attr("id");
       clearInterval(intervalID);
       $(".cpanel").removeClass("on").hide();
       $(".cpanel[id!='"+id+"panel']").hide();
       $(".clickarea img").css("opacity","0.6");
       
       if ($.browser.msie) {
            $("#"+id+"panel").addClass("on").show();
       } else {
            //$("#"+id+"panel").addClass("on").fadeIn(1000);
            $("#"+id+"panel").addClass("on").show();
       }
       $(this).children("img").css("opacity",1);
    });
    
    /*$(".content-tabs li").bind("click", function() {
        $(".content-panel").hide();
        $(".content-tabs li").removeClass("active");
        $(this).addClass("active");
        var id = $(this).attr("id");
        $(".controls.inline a").removeClass("active");
        $("#c"+id.substring(1)).addClass("active");       
        $("#"+id.substring(1)).show(); 
    });
    $(".controls.inline a").bind("click", function() {
        $(".content-panel").hide();
        $(".controls.inline a").removeClass("active");
        $(".content-tabs li").removeClass("active");
        $(this).addClass("active");
        var id = $(this).attr("id");
        $("#"+id.substring(1)).show(); 
        $("#t"+id.substring(1)).addClass("active"); 
    });*/
    
    // page tabs
    $(".page-tabs li").bind("click", function() {
       $(".tab-panel").hide();
       $(".page-tabs li").removeClass("active");
       $(this).addClass("active");
       var id = $(this).attr("id");
       $("#"+id+"_panel").show();
    });
    
    // date in header
    datetime();
    setInterval("datetime()",1000);
    
});

function datetime() {
    
    var now = new Date();
    var days = new Array('Sun','Mon','Tues','Wed','Thurs','Fri','Sat');
    var months = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec');
    var dates = ((now.getDate()<10) ? "0" : "")+ now.getDate();
    var time = ((now.getHours()<10) ? "0" : "") + now.getHours() + ":" + ((now.getMinutes()<10) ? "0" : "") + now.getMinutes();
    
    // Add suffix to date (1st, 2nd, 4th, etc.)
    var date = now.getDate()
    if (date==1) suffix=("st");
    else if (date==2) suffix=("nd");
    else if (date==3) suffix=("rd");
    else if (date==21) suffix=("st");
    else if (date==22) suffix=("nd");
    else if (date==23) suffix=("rd");
    else if (date==31) suffix=("st");
    else suffix=("th");
    
    function fourdigits(number)	{
      return (number < 1000) ? number + 1900 : number;
    }
    
    var today =  days[now.getDay()] + " " + months[now.getMonth()] + " " + dates + suffix + ", " + (fourdigits(now.getYear()))+ ". "+time;
    $("#datetime").html(today);
    
}

function cyclepanels() {
    var max = $(".wall li.large div").length;
    var id = $(".wall li.large div[class='cpanel on']").attr("id").substring(2);
    id = parseInt(id.replace(/panel/,''));
    $(".clickarea img").css("opacity","0.6");
    if (id+1 > max) { id = 1; } else { id = id + 1; }
    
    $("#cp"+id+"panel").parent().children("div.cpanel").removeClass("on").hide();
    $("#cp"+id+"panel").addClass("on");
    //if($.browser.msie) {
		$("#cp"+id+"panel").show();
	//} else {
	//    $("#cp"+id+"panel img").fadeIn("slow");
	//	$("#cp"+id+"panel").fadeIn("slow");
	//}
    
    $("#cp"+id+" img").css("opacity",1);
}
