$(document).ready(function() {
    // Equalize columns
    $("#sidebar, #content").equalizeCols();

    if ($('img').length) {
        $('img').load(function() {
            $("#sidebar, #content").equalizeCols();
        });
    } else {
        $("#sidebar, #content").equalizeCols();
    }

    // Fancy images for car sales
    $("div#carimageblock a").fancybox();

    // Mouseover carsales images
    $("div#carimageblock img").hover(function() {
        $(this).removeClass("carnormal");
        $(this).addClass("carhighlight");
    },function() {
        $(this).removeClass("carhighlight");
        $(this).addClass("carnormal");
    });

    // Highlight info/errors on load
    $("div.box_error td.message").effect("highlight", {color: '#ffcccc'}, 3000);
    $("div.box_info td.message").effect("highlight", {color: '#ccffcc'}, 3000);

    // Date pickers
    $('.datepicker').datepicker({
        dateFormat: 'D d M y',
        firstDay: 1,
        minDate: new Date((new Date()).getTime() + 86400000)
    });

    // Make external links appear in new window
    externalLinks();

    if (document.URL.toString().indexOf('/book') > 0) {
        $('.bookingchangeevent').click(function() {
            bookingChanged();
        });

        // Set a POST variable to indicate that JavaScript is available
        var jsEnabled = document.getElementById("jsFlag");
        if (jsEnabled) {
            jsEnabled.value = '1';
    	}

        bookingChanged();
    }
});

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}

function bookingChanged() {
	var isService = document.getElementById("servicecheck").checked;
    var isMot = document.getElementById("motcheck").checked;
    var isRepair = document.getElementById("repaircheck").checked;
    var isValet = document.getElementById("valetcheck").checked;
    var isWaiting = document.getElementById("isWaiting").checked;

    var rowMotTimes = $('#timemot');
    var rowCustomTimes = $('#timecustom');
    var rowWaiting = $('#waitingrow');

    if (!isService && isMot && !isRepair && !isValet) {
        rowWaiting.fadeIn();
    } else {
        rowWaiting.fadeOut();
    }

    if (!isService && isMot && !isRepair && !isValet && isWaiting) {
        rowMotTimes.fadeIn();
        rowCustomTimes.fadeOut();
    } else {
        rowMotTimes.fadeOut();
        rowCustomTimes.fadeIn();
    }

    // Service options
    var serviceRow = $('#servicetype');
    if (isService) {
        serviceRow.fadeIn();
    } else {
        serviceRow.fadeOut();
    }

    // Valet options
    var valetRow = $('#valettype');
    if (isValet) {
        valetRow.fadeIn();
    } else {
        valetRow.fadeOut();
    }
}

function popup(link, name) {
	if (!window.focus)
		return true;
	
	var href;
	if (typeof(link) == 'string')
		href = link;
	else
		href = link.href;
	
	window.open(href, name, 'width=600,height=600,scrollbars=yes,resizable=yes');
	return false;
}