﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
/// <reference path="jquery.form.js" />
/// <reference path="jquery.validate-vsdoc.js" />
/// <reference path="modal.js" />

$(document).ready(function () {

    var siteMode = "LIVE";

    var UkSite = "ljr.inkcreations.co.uk";
    var IrishSite = "ljrie.inkcreations.co.uk";

    if (siteMode == "LIVE") {
        UkSite = "www.lajolieronde.co.uk";
        IrishSite = "www.lajolieronde.ie";
    }

    var hostname = window.location.hostname;

    var pathName = window.location.pathname.toLowerCase();
    if ((pathName.indexOf("run-your-own-class/established-businesses/become-a-licensee") != -1 && $("#ClassAnswers").val() == "") ||
        (pathName.indexOf("run-your-own-class/become-a-tutor/enquire") != -1 && $("#TutorAnswers").val() == "")) {
        showModal();
    }


    $('#Area').change(function (evt) {
        //alert(hostname.indexOf(UkSite));
        if ($('#Area').val() == "United Kingdom" && hostname.indexOf(UkSite) != 0) {
            // Redirect to UK Site
            window.location.href = "http://" + UkSite + pathName;
        } else if ($('#Area').val() == "Ireland" && hostname.indexOf(IrishSite) != 0) {
            // Redirect to Irish site
            window.location.href = "http://" + IrishSite + pathName;
        }

        //alert($('#Area').val());
        //        if ($('#Area').val() == "United Kingdom") {
        //            $('#Location').removeClass("hidden");
        //            //$('#Location').addClass("input-validation-error");
        //            $('#Distance').removeClass("hidden");
        //            $('#IrishLocation').addClass("hidden");
        //            //$('#IrishLocation').removeClass("input-validation-error");
        //            $('span[data-valmsg-for="IrishLocation"]').removeClass("hidden");
        //            $('span[data-valmsg-for="Location"]').removeClass("hidden");
        //            $('#LocationLabel').html("<strong>Enter your location (and search radius) and hit \"GO\"</strong>");
        //        } else {
        //            $('#Location').addClass("hidden");
        //            //$('#Location').removeClass("input-validation-error");
        //            $('#Distance').addClass("hidden");
        //            $('#IrishLocation').removeClass("hidden");
        //            //$('#IrishLocation').addClass("input-validation-error");
        //            $('span[data-valmsg-for="IrishLocation"]').removeClass("hidden");
        //            $('span[data-valmsg-for="Location"]').removeClass("hidden");
        //            $('#LocationLabel').html("<strong>Choose your location and hit \"GO\"</strong>");
        //        }
    });

    $('#classSearchBtn').click(function (evt) {
        if ($('#Area').val() == "United Kingdom") {
            if ($('#Location').val() == "") {
                evt.preventDefault();
                $('#Location').addClass("input-validation-error");
            }
            else {
                $('#Location').removeClass("input-validation-error");
            }
        } else {

        }
    });

    $('#Location').keyup(function () {
        $('#Location').removeClass("input-validation-error");
    });

    $('ul.subnav-container').each(function () {
        $(this).append("<div class=\"subnav-bottom\"></div>");
    });

    $('#maincontent a[href^="http://"]').not('.class-search a.link').not('.find-a-class a.link')
	  .attr({
	      target: "_blank",
	      title: "This link will open in a new window"
	  })
	.append(' <img src=\"/content/images/nw.gif\" width=\"16\" height=\"12\" alt=\"New Window Icon\" />');

    $('#footer ul a[href^="http://"]')
	  .attr({
	      target: "_blank",
	      title: "This link will open in a new window"
	  })
	.append(' [^]');


    $('.scroll-pane').jScrollPane();

    bindSubNav();

    //bindModalLinks();

    if ($(".dateinput").length) {
        $(".dateinput").datepicker({
            dateFormat: 'dd/M/yy'
        });
    }

    //$(".dateinput").dateinput({
    //    format: 'dd/mmm/yyyy'
    //});


    $("input[name=Country]").change(function () {
        if ($("input[name=Country]:checked").val() == "UK") {
            if ($("#Postcode").val() == "IR1 1IR") {
                $("#Postcode").val("");
            }
            $("#postcodefield").show();
        } else {
            if ($("#Postcode").val() == "") {
                $("#Postcode").val("IR1 1IR");
            }
            $("#postcodefield").hide();
        }
    });

    $("input[name=NextStep]").change(function () {
        if ($("input[name=NextStep]:checked").val() == "Opportunities Available") {
            if ($("#AreaOfInterest").val() == ".") {
                $("#AreaOfInterest").val("");
            }
            $("#areafield").show();
        } else {
            $("#AreaOfInterest").val(".");
            $("#areafield").hide();
        }
    });

    $(".info-icon").tooltip({
        // place tooltip on the right edge
        position: "center right",

        // a little tweaking of the position
        offset: [-2, 10],

        // use the built-in fadeIn/fadeOut effect
        effect: "fade",

        // custom opacity setting
        opacity: 1,

        // there is no delay when the mouse is moved away from the trigger
        delay: 5

    }).click(function (evt) {
        evt.preventDefault();
    });

    $(".centre-info").tooltip({
        // custom positioning
        position: 'top right',

        // move tooltip a little bit to the right
        offset: [5, -87],

        // use the built-in fadeIn/fadeOut effect
        effect: "fade",

        // custom opacity setting
        opacity: 1,

        // there is no delay when the mouse is moved away from the trigger
        delay: 5

    }).click(function (evt) {
        evt.preventDefault();
    });

    $(".class-timetable").each(function () {
        var theParent = $(this).parent();
        var theTooltip = theParent.next();
        $(this).tooltip({
            tip: theTooltip,

            // custom positioning
            position: 'top right',

            // move tooltip a little bit to the right
            offset: [5, -87],

            // use the built-in fadeIn/fadeOut effect
            effect: "fade",

            // custom opacity setting
            opacity: 1,

            relative: true,

            // there is no delay when the mouse is moved away from the trigger
            delay: 5
        }).click(function (evt) {
            evt.preventDefault();
        });
    });

    $(".find-a-class").tooltip({
        tip: '#searchtip',

        // custom positioning
        position: 'center right',

        // move tooltip a little bit to the right
        offset: [0, -10],

        // use the built-in fadeIn/fadeOut effect
        effect: "fade",

        relative: true,

        // there is no delay when the mouse is moved away from the trigger
        delay: 5
    });

    var triggers = $(".modalInput").overlay({
        // custom top position
        top: 150,

        // some mask tweaks suitable for facebox-looking dialogs
        mask: {

            // you might also consider a "transparent" color for the mask
            color: '#ebecff',

            // load mask a little faster
            loadSpeed: 200,

            // very transparent
            opacity: 0.5
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: false

    });


    //placeholderHack();
});

// Modal
//#region Modal
var bindModalLinks = function () {
	var callback = function (textStatus) {
		bindValidation();
	}
	$.modal(".modalLink", callback);
};
//#endregion


function placeholderHack()
{
	if (!Modernizr.input.placeholder)
	{
		$("input").each(
			function ()
			{
				if ($(this).attr("placeholder") != "" && ($(this).val() == "" || $(this).val() == $(this).attr("placeholder")))
				{
					$(this).val($(this).attr("placeholder"));
					$(this).focus(function ()
					{
						if ($(this).val() == $(this).attr("placeholder")) $(this).val("");
					});
					$(this).blur(function ()
					{
						if ($(this).val() == "") $(this).val($(this).attr("placeholder"));
					});
				}
			});
	}
}

function bindSubNav() {
	$(".hassubnav").each(function () {
		//$(".hassubnav").hoverIntent(showNav, hideNav);
		var theTooltip = $(this).children("ul.subnav-container");
		$(this).tooltip({
			tip: theTooltip,

			// custom positioning
			position: "bottom right",

			// move tooltip a little bit to the right
			offset: [-45, -10],

			// use the built-in fadeIn/fadeOut effect
			//effect: "fade",

			relative: true,

			opacity: 1,

			// there is no delay when the mouse is moved away from the trigger
			delay: 0
		});
	});
}

//function showNav() { $(this).find("ul").show("fast"); }
//function hideNav() { $(this).find("ul").hide("fast"); }


function showModal() {
	// select the overlay element - and "make it an overlay"
	$("#prompt").overlay({

		// custom top position
		top: 150,

		// some mask tweaks suitable for facebox-looking dialogs
		mask: {

			// you might also consider a "transparent" color for the mask
			color: '#ebecff',

			// load mask a little faster
			loadSpeed: 200,

			// very transparent
			opacity: 0.9
		},

		// disable this for modal dialog-type of overlays
		closeOnClick: false,
		closeOnEsc: false,

		// load it immediately after the construction
		load: true

	});
}
