﻿/// <summary>Uniworld JQuery Plugins.</summary>
function resizeToFit(iframe) {
    var innerFrame = document.getElementById(iframe.id);
    var innerDoc = (innerFrame.contentDocument) ? innerFrame.contentDocument : innerFrame.contentWindow.document;
    if (innerFrame != null) {
        //var innerFrame = $("#"+iframe.id);
        var contentHeight = 0;
        var contentWidth = 0
        var windowHeight = 0
        if ($.browser.mozilla) {
            contentHeight = document.getElementById("WindowFrame").contentWindow.document.body.scrollHeight;  //innerDoc.body.scrollHeight;
            contentWidth = document.getElementById("WindowFrame").contentWindow.document.body.scrollWidth;  //innerDoc.body.scrollWidth;
            //alert("Height: " + contentHeight + "| Width: " + contentWidth);
        } else {
            contentHeight = WindowFrame.document.body.scrollHeight;
            contentWidth = WindowFrame.document.body.scrollWidth;
        }

        if ($.browser.msie && $.browser.version <= 8) {
            windowHeight = document.documentElement.clientHeight;        
        } else {
            windowHeight = window.innerHeight;
        }


        if (contentHeight < windowHeight) {
            $("#WindowFrame").attr("scrolling", "no");
            if ($.browser.mozilla) {
                iframe.height = contentHeight;
                iframe.width = contentWidth;
            } else {
                iframe.height = contentHeight;
                iframe.width = contentWidth;
            }

            $("#WindowFrame").center();
            var position = $("#WindowFrame").position();
            var o = $("#WindowFrame").offset();
            var offset = (($("#WindowFrame").innerWidth() + position.left) - $("#windowPane button.btnCloseWnd").width()) - 6;

            if ($.browser.msie) {                             
                var src = $("#WindowFrame").attr("src");
                if ($.browser.version < 8) {
                    offset = (($("#WindowFrame").innerWidth() + position.left) - $("#windowPane button.btnCloseWnd").width()) - 12;
                }
                

                if (src.indexOf(".jpg") == -1) {
                    iframe.height = contentHeight + 25;
                    iframe.width = contentWidth + 25;
                    offset = offset + 25;
                } else {
                    iframe.height = contentHeight;
                    iframe.width = contentWidth;
                   // iframe.height = contentHeight;  // innerFrame.document.body.clientHeight + 1;
                    //iframe.width = innerFrame.document.body.clientWidth;
                }

                
                
                //var offset = (($("#WindowFrame").innerWidth() + position.left) - $("#windowPane button.btnCloseWnd").width()) - 20 + "px";
            }

            
            //alert("framewidth: " + offset);
            $("#windowPane button.btnCloseWnd").css({ 'z-index': '1100', position: 'absolute', margin: 0, top: position.top + 12 + "px", left: offset + "px" });
            $("#windowPane button.btnCloseWnd").removeClass("hidden");
            $("#WindowFrame").removeClass("invisible");
        } else {
            iframe.height = $(window).height() - 70;
            iframe.width = contentWidth + 30;

            $("#WindowFrame").center();
            var position = $("#WindowFrame").position();
            var o = $("#WindowFrame").offset();
            var offset = (($("#WindowFrame").innerWidth() + position.left) - $("#windowPane button.btnCloseWnd").width()) - 15 + "px";
            //alert("framewidth: " + offset);
            $("#windowPane button.btnCloseWnd").css({ 'z-index': '1100', position: 'absolute', margin: 0, top: position.top + 6, left: offset });
            $("#windowPane button.btnCloseWnd").removeClass("hidden");
            $("#WindowFrame").removeClass("invisible");
        }
        $("#loader").remove();
    }
}

function TestFunction() {
    resizeToFit(document.getElementById("WindowFrame"));
}

$(document).ready(function () {
    http: //localhost:51125/Destinations/Europe
    //  Initialize auto-hint fields
    $('INPUT.auto-hint, TEXTAREA.auto-hint').focus(function () {
        if ($(this).val() == $(this).attr('title')) {
            $(this).val('');
            $(this).removeClass('auto-hint');
        }
    });

    $('INPUT.auto-hint, TEXTAREA.auto-hint').blur(function () {
        if ($(this).val() == '' && $(this).attr('title') != '') {
            $(this).val($(this).attr('title'));
            $(this).addClass('auto-hint');
        }
    });

    $('INPUT.auto-hint, TEXTAREA.auto-hint').each(function () {
        if ($(this).attr('title') == '') { return; }
        if ($(this).val() == '') { $(this).val($(this).attr('title')); }
        else { $(this).removeClass('auto-hint'); }
    });

    //    if (document.getElementById("WindowFrame") == null) {
    //        addWindowDiv();
    //    }

    $("a.help-msg").click(function () {
        msg = $(this).attr("data-help");
        window.alert(msg);
        return false;
    });

    $("#windowPane").live('click', function () {
        //$(this).addClass("hidden");
        //alert("Frame Close");
        $(this).remove();
    });

    $("a.openPopup").click(function () {

        //        if (document.getElementById("WindowFrame") == null) {
        //            addWindowDiv();
        //        }


        var target = $(this).attr("href");
        if ($(this).attr("href").indexOf("?") != -1) {
            target += '&w=1';
        } else {
            target += '?w=1';
        }

        if (target.indexOf(".jpg") != -1) {
            target = "/View_Image" + target;
        }

        //if (document.getElementById("WindowFrame") == null) {
        //	addWindowDiv();
        //}
        addWindowDiv(target)
        var innerFrame = document.getElementById("WindowFrame");


        //        if (innerFrame.src) {
        //            //innerFrame.src = target;
        //        } else if (innerFrame.contentWindow != null && innerFrame.contentWindow.location != null) {
        //            innerFrame.contentWindow.location = target;
        //        } else {
        //            innerFrame.setAttribute("src", target);
        //                }



        innerPaneWidth = innerFrame.width + "px";
        //alert(innerPaneWidth);

        //        if ($(this).attr("data-height") != null) {
        //            $("div#PopupPane").css("height", $(this).attr("data-height") + "px");
        //            innerFrame.setAttribute("height", $(this).attr("data-height") - 20);
        //        }

        //        if ($(this).attr("data-width") != null) {
        //            innerPaneWidth = $(this).attr("data-width") + "px";
        //            innerFrame.setAttribute("width", $(this).attr("data-width") - 10);
        //        }

        // $("div#PopupPane").css("width", innerPaneWidth);
        // $("div#PopupPane").center();
        //$("#WindowFrame").center();

        if (innerFrame != null) {
            $("div#windowPane").toggleClass("hidden");
        } else {
            $("#Signup").center();
        }
        return false;
    });

    //    $("#WindowFrame").live('load', function () {
    //        alert("Loaded");
    //    });


    function addWindowDiv(src) {
        //var onload = "this.style.height=" + WindowFrame.document.body.scrollHeight + 5 + ";";
        //$("body").append("")

        var onload = "resizeToFit(this);";
        var append = '<div id="windowPane" class="hidden"><img src="/images/icons/loader.gif" id="loader" /><button name="btnCloseWnd" class="btnCloseWnd hidden">X</button><iframe name="WindowFrame" id="WindowFrame" onload="' + onload + '" src="' + src + '" frameborder="0" marginheight="0" marginwidth="0"scrolling="auto" horizontalscrolling="no" class="invisible" ></iframe></div>'
        //alert(append);
        
        $("body").append(append);
        $("#loader").center();
        //alert("build");
    }


    $("a.Confirm").click(function () {

        confirmMsg = "Do you wish to contiune";
        if ($(this).attr("confirm")) { confirmMsg = $(this).attr("confirm") }

        var response = confirm(confirmMsg);

        if (response) { return true; }

        return false;
    });

    $("a.windowed").click(function () {

        var window
        alert("windowed");

        return false;
    });

    $(".zoomer").imageZoom({
        small: $("#DeckPlan").attr("src"),
        original: $("a.zoom").attr("href"),
        viewpoint: {
            width: 250,
            height: 150
        }
    });


    $("select option").mouseover(function () {
        //$(this).toggleClass("hover");
    })

    $("a").click(function () {
        if (getFileExtension($(this).attr("href")) == "pdf") {
            $(this).attr("target", "_blank");
        }
    });

    $(".openNewWindow").click(function () {
        var href = $(this).attr("href");
        window.open(href, 'map_window', 'status=no,scrollbars=yes,resizable=yes,location=no,width=750,height=560,left=100,top=50');
        return false;
    });

    $(".virtualTourWindow").click(function (e) {
        var URL = $(this).attr("href");
        if ($.browser.msie) {
            window.open(URL, 'Course', 'location=0,toolbar=0,status=0,scrollbars=0,resizable=1,width=1010,height=690');
        }
        else {
            window.open(URL, 'Course', 'toolbar=0,status=0,scrollbars=0,resizable=1,width=1000,height=690');
        }
        return false;
    });

    function getFileExtension(filename) {
        var ext = /^.+\.([^.]+)$/.exec(filename);
        return ext == null ? "" : ext[1];
    }

    var labelText = $("#SearchInputLabel").text();
    $("#SearchInput").val(labelText);

    $("#SearchInput").focus(function () {
        if ($(this).val() == labelText) {
            $(this).val("");
            $(this).toggleClass("greyout");
        }
    });
    $("#SearchInput").blur(function () {
        if ($(this).val() == "") {
            $(this).val(labelText);
            $(this).toggleClass("greyout");
        }
    });
    /*$("select#SelectLangauge option").click(function() {
    if ($(this).val() != "0") {
    $("input[name=Selected]").val($(this).val());
    //$("form").attr("action","../" + $(this).val());
    //alert($(this).val());
    $("form").submit();
    }
    });*/

    function changeMarket() {
        if ($("select#SelectLangauge").val() != "0") {
            $("input[name=Selected]").val($(this).val());
            //$("form").attr("action","../" + $(this).val());
            //alert($(this).val());
            $("form#SelectLangauge").submit();
        }
    }

    $("#NewsLetterContainer a").click(function () {
        $("#SignupContainer").toggleClass("hidden");
        $("a#NoThanks").addClass("hidden");
        return false;
    });

    $("a.windowOpen").click(function () {
        $("div#windowPane").toggleClass("hidden");
        return false;
    });

    $("a.policyOpen").click(function () {
        var target = $(this).attr("href");
        var policyFrame = document.getElementById("PolicyFrame");

        if (policyFrame != null) {

            if (policyFrame.src) {
                policyFrame.src = target;
            } else if (policyFrame.contentWindow != null && policyFrame.contentWindow.location != null) {
                policyFrame.contentWindow.location = target;
            } else {
                policyFrame.setAttribute("src", target);
            }

            $("div#windowPane").toggleClass("hidden");

        }
        return false;
    });


    //    $("#SignupContainer").live('click', function () {
    //        //alert("Test");
    //        $(this).addClass("hidden");
    //        reset();
    //        var exdate = new Date();
    //        exdate.setDate(exdate.getDate() + 3600);
    //        expires = exdate.toUTCString();
    //        document.cookie = "AutoPopup=nothanks;expires=" + expires;

    //    });
    $("button.btnCloseWnd").click(function () {
        if ($("#WindowFrame").size() < 1) {
            //alert("Close Signup");
            $(this).parent().parent().toggleClass("hidden");
            reset();
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + 3600);
            expires = exdate.toUTCString();
            document.cookie = "AutoPopup=nothanks;expires=" + expires;
        } else {
            //alert("Found: " + $("#WindowFrame").size());
            //alert("Frame Close");
            $("#windowPane").remove();
        }
        return false;
    });

    $("form#NewsletterForm").submit(function () {
        var error = false;
        reset();

        //Check for First Name
        if ($("#NewsletterFirstName").val() == "First Name" || $("#NewsletterFirstName").val().length < 2) {
            $("#errorlist").append("<li>Please enter your First Name</li>");
            $("#NewsletterFirstName").addClass("error");
            error = true;
        }
        //Check for Last Name
        if ($("#NewsletterLastName").val() == "Last Name" || $("#NewsletterLastName").val().length < 2) {
            $("#errorlist").append("<li>Please enter your Last Name</li>");
            $("#NewsletterLastName").addClass("error");
            error = true;
        }

        //Check for valid email address
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if (reg.test($("#NewsletterEmail").val()) == false) {
            $("#errorlist").append("<li>Enter a valid Email Address</li>");
            $("#NewsletterEmail").addClass("error");

            error = true;
        } else if ($("#NewsletterEmail").val() != $("#NewsletterEmailConfirm").val()) {
            $("#errorlist").append("<li>Confirm your Email Address</li>");
            $("#NewsletterEmailConfirm").addClass("error");
            error = true;
        }

        if (error == true) {
            $("#errorlist").removeClass("hidden");
        } else {

            $.post($("#NewsletterForm").attr("action"), {
                FirstName: $("#NewsletterFirstName").val(),
                LastName: $("#NewsletterLastName").val(),
                Email: $("#NewsletterEmail").val(),
                EmailConfirm: $("#NewsletterEmailConfirm").val(),
                OptinListID: $("#OptinListID").val(),
                Source: "Sigup-Form:" + location.href
            },
						function (data) {
						    $("#Signup form").html("<p>" + data + "</p>");
						    var exdate = new Date();
						    exdate.setDate(exdate.getDate() + 3600);
						    expires = exdate.toUTCString();
						    document.cookie = "AutoPopup=nothanks;expires=" + expires;
						}
						, "html");
        }
        return false;
    });

    (function ($) {

        $.fn.extend({
            center: function () {
                return this.each(function () {
                    //var top = ($(window).height() - $(this).outerHeight()) / 10;
                    //var left = ($(window).width() - $(this).outerWidth()) / 2;
                    //$(this).css({ position: 'absolute', margin: 0, top: (top > 0 ? top : 0) + 'px', left: (left > 0 ? left : 0) + 'px' });
                    var windowWidth = document.documentElement.clientWidth;
                    var windowHeight = document.documentElement.clientHeight;
                    //                    if (($(this).outerHeight() * 1.25) >= windowHeight) {
                    //                        var topPosition = 10;
                    //                    } else {
                    //                        var topPosition = "25%";
                    //                    }
                    var topPosition = windowHeight / 2 - ($(this).height() / 2);
                    var leftPosition = windowWidth / 2 - ($(this).outerWidth() / 2);
                    //alert($(this).outerWidth());
                    //var leftPosition = -(WindowFrame.document.body.scrollWidth / 2)
                    //alert("Top: " + $(this).outerHeight() + "Window Height:" + window.innerHeight);

                    $(this).css({ "position": "absolute", "top": topPosition, "left": leftPosition });
                    //var pos = $(this).position();
                    //alert(pos.left());
                });
            }
        });
    })(jQuery);





    function reset() {
        var error = false;
        $("#NewsletterEmail").removeClass("error");
        $("#NewsletterEmailConfirm").removeClass("error");
        $("#NewsletterFirstName").removeClass("error");
        $("#NewsletterLastName").removeClass("error");
        $("#errorlist").html("");
        if ($("#errorlist").hasClass("hidden") != true) { $("#errorlist").addClass("hidden") }
    }



    /*GA Tracking */
    var lnks = document.getElementsByTagName('a');
    var regLocal = new RegExp("^https?:\\/\\/" + window.location.hostname);
    var i, elLnk;
    for (i = 0; i < lnks.length; i++) {

        elLnk = lnks[i];
        if (!/^https?:\/\//.test(elLnk.href) || regLocal.test(elLnk.href)) {
            continue;
        }
        if (elLnk.attachEvent) {
            elLnk.attachEvent('onclick', onExternalClick);

        }
        else if (elLnk.addEventListener) {
            elLnk.addEventListener('click', onExternalClick, false);

        }
    }
    function onExternalClick(Evt) {
        var elLnk = Evt ? (Evt.target ? Evt.target : Evt.srcElement) : event.srcElement;
        trackExternal(elLnk);
        return true;
    }

    function trackExternal(elLink) {
        var path = elLink.href.replace('/^https?:\/\//', '/external/');
        if (path && path.indexOf('/external') == 0) {

            //pagetracker._trackPageview(path);
        }
        return true;
    }

    $('a.AddFriendButton').click(function () {
        $.post("/Community/Services/AddFriend", { userID: $(this).attr("rel") },
				function (data) {
				    $(this).toggleClass("hidden");
				    $("a." + data).toggleClass("hidden");

				});
    });

    $("input.calendar").datepicker({
        showOn: "button",
        buttonImage: "/images/icons/calendar.png",
        buttonImageOnly: true
    });



});

//Track the s_ref from Facebook
var s_ref = "";
var hrefsplit = window.location.href.split("?");
var source_URL = hrefsplit[0];

if (hrefsplit.length == 2) {
	if (refmatch = hrefsplit[1].match(/\bs_ref=([^&]+)/gi)) {
		for (var i = 0; i < refmatch.length; i++) {
			var refsplit = refmatch[i].split("=");
			if (refsplit.length == 2) {
				if (refsplit[0].toLowerCase() == "s_ref") {
					s_ref = refsplit[1];
					break;
				}
			}
		}
	}
}

if (s_ref != "") {
	$.post("/community/services/TrackRef", { id: s_ref },
		function (data) {
			alert("Tracking sent");
		}
		, "html");
}
	   
		var addthis_config =
		{
			ui_click: true,
			ui_508_compliant: true,
			ui_offset_top: 15,
			ui_offset_left: 15
		}

		function changeMarket() {
			if ($("select#SelectLangauge").val() != "0") {
				$("input[name=Selected]").val($("select#SelectLangauge").val());
				//$("form").attr("action","../" + $(this).val());
				//alert($(this).val());
				document.MarketSelect.submit();
			}
		}

		var BrowserDetect = {
			init: function() {
				this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
				this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
				this.OS = this.searchString(this.dataOS) || "an unknown OS";
			},
			searchString: function(data) {
				for (var i = 0; i < data.length; i++) {
					var dataString = data[i].string;
					var dataProp = data[i].prop;
					this.versionSearchString = data[i].versionSearch || data[i].identity;
					if (dataString) {
						if (dataString.indexOf(data[i].subString) != -1)
							return data[i].identity;
					}
					else if (dataProp)
						return data[i].identity;
				}
			},
			searchVersion: function(dataString) {
				var index = dataString.indexOf(this.versionSearchString);
				if (index == -1) return;
				return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
			},
			dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
			dataOS: [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.userAgent,
			subString: "iPhone",
			identity: "iPhone/iPod"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

		};
		BrowserDetect.init();
