﻿
//
// Browser Detection
//
isMac = (navigator.appVersion.indexOf("Mac") != -1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all) && (isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all) && (navigator.appVersion.indexOf("MSIE 4.") != -1)) ? true : false;
IE5 = ((document.all) && (navigator.appVersion.indexOf("MSIE 5.") != -1)) ? true : false;
IE6 = ((document.all) && (navigator.appVersion.indexOf("MSIE 6.") != -1)) ? true : false;
IE7 = ((document.all) && (navigator.appVersion.indexOf("MSIE 7.") != -1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape') != -1) ? true : false;

/* For postback and .net validation */
window.scrollTo = function () { };

/* BEGIN Carousel functions */

var tOffset;
var thumbLength;
var scrollLength;
var isscroll;
var index;
var current_direction;
var scroller;
var advertIndex;

function initCsl(intIndex) {
    scroller = document.getElementById("scroller_" + intIndex);
    tOffset = document.getElementById("tOffset_" + intIndex);
    thumbLength = document.getElementById("ThumbLength_" + intIndex);
    scrollLength = document.getElementById("ScrollLength_" + intIndex);
    isscroll = document.getElementById("IsScroll_" + intIndex);
    index = document.getElementById("Index_" + intIndex);
    current_direction = document.getElementById("Current_direction_" + intIndex);

    advertIndex = intIndex;
}

function scrollingLeft() {
    intToffset = parseFloat(tOffset.value);

    if (intToffset == 0) {
        scroller.style.marginLeft = intToffset + "px";
        isscroll.value = false;
        return false;
    }
    else if (intToffset % 70 == 0 && isscroll.value == "true") {
        scroller.style.marginLeft = intToffset + "px";
        isscroll.value = false;
        clearTimeout(doscroll);
    }
    else {
        if (intToffset % 70 == 0)
            Fade("left");

        isscroll.value = true;
        tOffset.value = intToffset + 17.5;
        scroller.style.marginLeft = tOffset.value + "px";
        doscroll = setTimeout("scrollingLeft()", 20);
    }
}

function scrollingRight(MaxRight) {
    intToffset = parseFloat(tOffset.value);

    if (intToffset <= MaxRight) {
        scroller.style.marginLeft = tOffset.value + "px";
        isscroll.value = false;
        return false;
    }
    else if (intToffset % 70 == 0 && isscroll.value == "true") {
        scroller.style.marginLeft = tOffset.value + "px";
        isscroll.value = false;
        clearTimeout(doscroll);

        if (intToffset < -1140) {
            isscroll.value = false;
        }
    }
    else {
        if (intToffset % 70 == 0)
            Fade("right");

        isscroll.value = true;
        scroller.style.marginLeft = tOffset.value + "px";
        tOffset.value = intToffset - 17.5;
        doscroll = setTimeout("scrollingRight(" + MaxRight + ", '" + scroller + "')", 20);
    }
}

function Fade(direction) {
    // parse int
    intIndex = parseInt(index.value);

    if (current_direction.value != direction && current_direction.value != "") {
        if (intIndex > 0) {
            if (direction == "right")
                index.value = intIndex + 1;
            else
                index.value = intIndex - 1;
        }
    }

    // reparse
    intIndex = parseInt(index.value);

    if (direction == "right") {
        $("#t_" + advertIndex + "_" + intIndex).fadeTo('fast', 0.01);
        $("#t_" + advertIndex + "_" + (intIndex + 4)).fadeTo('fast', 1);

        current_direction.value = "right";

        index.value = intIndex + 1;
    }
    else {
        $("#t_" + advertIndex + "_" + intIndex).fadeTo('fast', 1);
        $("#t_" + advertIndex + "_" + (intIndex + 4)).fadeTo('fast', 0.01);

        current_direction.value = "left";

        if (intIndex > 0)
            index.value = intIndex - 1;
    }
}

function changeimage(imgurl, MainImageIndex, name, ImgIndex, count, objImage) {
    $("#mainphoto_" + MainImageIndex).toggle();
    $("#mainphoto_" + MainImageIndex).html('<img src=\"' + imgurl + '\" alt=\"' + name + '\" title=\"' + name + '\" class=\"carousel-placeholder\" />');
    $("#mainphoto_" + MainImageIndex).fadeIn('slow');

    //$('#photodesc').html('<h2>' + name + '</h2>' + caption + '<div class="pagenum"> ' + ImgIndex + ' / ' + count + '</div>');
}
function getObj(id) {
    if (document.getElementById) {
        this.obj = document.getElementById(id);
        this.style = document.getElementById(id).style;
    }
    else if (document.all) {
        this.obj = document.all[id];
        this.style = document.all[id].style;
    }
    else if (document.layers) {
        this.obj = document.layers[id];
        this.style = document.layers[id];
    }
    else {
        alert("Can not find the object");
    }
}


/* END carousel functions */





// initialise flash text (sifr)
var flashtext = {
    src: 'images/TEMP/sifr/freescript.swf'
};

// attach click function to carousel thumbnail images and update main carousel image with src when click occurs
function carouselThumbnail() {
    $('.jcarousel-skin-tango li').click(function () {
        var src = $(this).find('span').text();
        if (src.length != 0) {
            $(this).parents('.ad-photos:eq(0)').find('img:eq(0)').attr('src', src);
        }
    });
}

// initialise Carousel
function carouselInit() {
    jQuery('.jcarousel-skin-tango').jcarousel();
}

// Get the element height
function getElementHeight() {
    if (document.getElementById) {
        var elem = document.getElementById("pageheader");
    } else if (document.all) {
        var elem = document.all["pageheader"];
    }
    //if (op5) { 
    //xPos = elem.style.pixelHeight;
    //} else {
    var xPos = 0;

    if (elem != null)
        xPos = elem.offsetHeight;
    //}

    return xPos + 65;
}

//initialise Ad Rollover
function carouselAdRollover() {
    $('.carousel-placeholder').click(function (e) {
        if ($('#overlay').length < 1) {
            var html = $(this).parents('.ads-container:eq(0)').html();

            // alert(html);
            /// <reference path="file:///C:\Users\SMANNE~1.WIL\AppData\Local\Temp\gi5jvfv1.bmp" />

            // Need to unbind previous jcarousel before rebinding it
            var topVal = $(document).scrollTop() + 200;
            var overlay = '<div class="ads-standard-wrapper overlay jqmWindow" id="overlay" style="position:absolute;margin-left:-300px;left:50%;font-size:1.2em;top:' + topVal + 'px;">' + html + '</div>';
            $(this).parents('body').prepend(overlay);
            var close = function (hash) { hash.w.fadeOut('2000', function () { $('#overlay').remove(); $('.jqmOverlay').remove(); }); };
            var open = function (hash) { hash.w.fadeIn('2000'); };
            $('#overlay').jqm({ onHide: close, onShow: open }).jqmShow();
            carouselThumbnail();
        }
    });
}


function MoreDetailsRollover() {
    $('.moreinfoprem').click(function (e) {
        if ($('#overlay').length < 1) {

            var html;

            if ($('.ads-intro').length > 0) {
                //premier ads
                html = $(this).parents('.ads-intro').find('.ads-container:eq(0)').html();
                //alert(1);
            }
            else if ($('.mod-group').length > 0) {
                //homepage ads
                html = $(this).parents('.mod-group').find('.ads-container:eq(0)').html();
                //alert(2);
            }
            else if ($('.ads-intro2').length > 0) {
                //homepage ads
                html = $(this).parents('.ads-intro2').find('.ads-container:eq(0)').html();
                //alert(3);
            }
            else {
                //general ads
                html = $(this).parents('.ads-container:eq(0)').html();
                //alert(4);
            }

            // Need to unbind previous jcarousel before rebinding it
            var topVal = $(document).scrollTop() + 200;
            var overlay = '<div class="ads-standard-wrapper overlay jqmWindow" id="overlay" style="position:absolute;margin-left:-300px;left:50%;font-size:1.2em;top:' + topVal + 'px;">' + html + '</div>';
            $(this).parents('body').prepend(overlay);
            var close = function (hash) { hash.w.fadeOut('2000', function () { $('#overlay').remove(); $('.jqmOverlay').remove(); }); };
            var open = function (hash) { hash.w.fadeIn('2000'); };
            $('#overlay').jqm({ onHide: close, onShow: open }).jqmShow();

            $("#overlay .stable-lnk").colorbox({
                innerWidth: 241,
                innerHeight: 150,
                iframe: true,
                scrolling: false,
                onComplete: function () { $(this).colorbox.resize(); },
                onOpen: function () { $(this).colorbox({ href: "/addToStable.aspx?adId=" + $(this).attr("id") }) }
            });

            $("#overlay .stableemail-lnk").colorbox({
                innerWidth: 300,
                innerHeight: 325,
                iframe: true,
                scrolling: false,
                //onComplete: function () { $(this).colorbox.resize(); },
                onOpen: function () { $(this).colorbox({ href: "/submitenquiry.aspx?aid=" + $(this).attr("id") }) }
            });
            carouselThumbnail();
        }
    });
    $('.moreinfo').click(function (e) {
        if ($('#overlay').length < 1) {

            var html;
            if ($('.ads-intro').length > 0) {
                //premier ads
                html = $(this).parents('.ads-intro').find('.ads-container:eq(0)').html();
                //alert(1);
            }
            else if ($('.ads-intro2').length > 0) {
                //premier ads
                html = $(this).parents('.ads-intro2').find('.ads-container:eq(0)').html();
                //alert(1);
            }
            else if ($('.mod-group').length > 0) {
                //homepage ads
                html = $(this).parents('.mod-group').find('.ads-container:eq(0)').html();
                //alert(2);
            }
            else if ($(".topthumbs").length > 0) {
                html = $(this).parents('.topthumbs').find('.ads-container:eq(0)').html();
            }
            else {
                //general ads
                html = $(this).parents('.ads-container:eq(0)').html();
                //alert(4);
            }

            // Need to unbind previous jcarousel before rebinding it
            var topVal = $(document).scrollTop() + 200;
            var overlay = '<div class="ads-standard-wrapper overlay jqmWindow" id="overlay" style="position:absolute;margin-left:-300px;left:50%;font-size:1.2em;top:' + topVal + 'px;">' + html + '</div>';
            $(this).parents('body').prepend(overlay);
            var close = function (hash) { hash.w.fadeOut('2000', function () { $('#overlay').remove(); $('.jqmOverlay').remove(); }); };
            var open = function (hash) { hash.w.fadeIn('2000'); };
            $('#overlay').jqm({ onHide: close, onShow: open }).jqmShow();
            carouselThumbnail();
        }
    });
}

function MoreDetailsMultiRollover() {
    $('.multimoreinfo').click(function (e) {
        if ($('#overlay').length < 1) {

            var html;

            if ($('.multi-advert-container').length > 0) {
                //multi ads
                html = $(this).parents('.multi-advert-container').find('.ads-container:eq(0)').html();
            }

            // Need to unbind previous jcarousel before rebinding it
            var topVal = $(document).scrollTop() + 200;
            var overlay = '<div class="ads-standard-wrapper overlay jqmWindow" id="overlay" style="position:absolute;margin-left:-300px;left:50%;font-size:1.2em;top:' + topVal + 'px;">' + html + '</div>';
            $(this).parents('body').prepend(overlay);
            var close = function (hash) { hash.w.fadeOut('2000', function () { $('#overlay').remove(); $('.jqmOverlay').remove(); }); };
            var open = function (hash) { hash.w.fadeIn('2000'); };
            $('#overlay').jqm({ onHide: close, onShow: open }).jqmShow();
            carouselThumbnail();
        }
    });
}

// initialise nav see more links
function seeMoreNav() {
    var liCount = $("ul.nav li.mainlvl").size();
    for (i = 1; i < liCount; ++i) {
        if ($("ul.nav li#mainlvl" + i + " ul.sublvl" + i + " li").size() > 4) {
            var strNavHeader = $("ul.nav li#mainlvl" + i + " a.on").text();
            $("ul.nav li#mainlvl" + i + " ul.sublvl" + i + " li:gt(3)").hide();
            $("ul.nav li#mainlvl" + i + " ul.sublvl" + i + " li:eq(3)").after('<li class="more-links" id="morelnk' + i + '"><a href="javascript:void(0)" onclick="showLi(this, ' + i + ')">See More ' + strNavHeader + '</a></li>')
        }
    }
}

function showLi(obj, strobj) {
    //alert(obj.id);
    //alert(strobj);
    $(obj).remove();
    $("ul.nav li#mainlvl" + strobj + " ul.sublvl" + strobj + " li:gt(3)").show();
    return false;

}
// initialise Toggle
function Toggle() {
    $('dt.toggle').css('cursor', 'pointer').hover(
                  function () { $(this).css('text-decoration', 'underline'); },
                  function () { $(this).css('text-decoration', 'none'); }
                ).click(function () {
                    $(this).toggleClass('open').next().slideToggle('fast');
                });
}

function initCountrySelector() {
    $('ul.countryselector').Zoomer({
        speedView: 400,
        speedRemove: 400,
        altAnim: true,
        initWidth: 40,
        initHeight: 28,
        zoomWidth: 76,
        zoomHeight: 55,
        initPadding: 10
    });
}

$().ready(function () {
    initInfo();
    //carouselInit();
    //carouselThumbnail();
    //carouselAdRollover();
    //seeMoreNav();
    //Toggle();
    initCountrySelector();
    initFakeSelectMenu();
    initAutoSuggest();
    initAdvancedLink();
    initSearchForm();
});

// initialise
$(window).load(function () {
    MoreDetailsRollover();
    MoreDetailsMultiRollover();

    $(".stable-lnk").colorbox({
        innerWidth: 241,
        innerHeight: 150,
        iframe: true,
        scrolling: false,
        onComplete: function () { $(this).colorbox.resize(); },
        onOpen: function () { $(this).colorbox({ href: "/addToStable.aspx?adId=" + $(this).attr("id") }) }
    });

    $(".stableemail-lnk").colorbox({
        innerWidth: 300,
        innerHeight: 325,
        iframe: true,
        scrolling: false,
        //onComplete: function () { $(this).colorbox.resize(); },
        onOpen: function () { $(this).colorbox({ href: "/submitenquiry.aspx?aid=" + $(this).attr("id") }) }
    });

    $("#cvcinfo").colorbox({ width: "80%", height: "80%", iframe: true });

    $("a[data-role='ShowMap']").colorbox({
        innerWidth: 600,
        innerHeight: 400,
        iframe: true,
        onOpen: function () { $(this).colorbox({ href: "/members/ClickableMap.aspx" }); },
        onClosed: function () { location.reload(true); }
    });
});

$(document).ready(function () {
    $("ul.nav").superfish({ speed: 4 });    // initialise menu
});

//$(window).scroll(function() {
//    var offset = $("#floatnav").offset();
//    var nav = new getObj("#floatnav");
//    var scrollY = window.pageYOffset;
//    var scrollY = document.documentElement.scrollTop;

//    var headHeight = getElementHeight();

//    if (scrollY > headHeight) {
//        $("#floatnav").css("top", (scrollY - headHeight - 10) + "px");
//    }
//    else {
//        //$("#nav").css("top", headHeight + "px");
//        $("#floatnav").css("top", "65px");
//    }

//});


// MULTI FILE UPLOAD

var Id = 0;
//    var MAX;
//    var DivFiles;
//    var DivListBox;

// Adds a new file input
function Add(DivFiles, DivListBox, hdnobj, prefix) {
    var MAX = 1;

    if (hdnobj != null)
        MAX = hdnobj.value;

    var IpFile = GetTopFile(DivFiles);
    if (IpFile == null || IpFile.value == null || IpFile.value.length == 0) {
        alert('Please select a file to add.');
        return;
    }
    var NewIpFile = CreateFile(DivFiles, DivListBox, hdnobj, prefix);
    DivFiles.insertBefore(NewIpFile, IpFile);
    if (MAX != 0 && GetTotalItems(DivListBox) + 1 >= MAX) {
        NewIpFile.disabled = true;
    }
    IpFile.style.display = 'none';
    DivListBox.appendChild(CreateItem(IpFile, DivFiles, DivListBox, hdnobj, prefix));
}

function CreateFile(DivFiles, DivListBox, hdnobj, prefix) {
    Id = GetTotalItems(DivListBox) + 1;

    var IpFile = document.createElement('input');
    IpFile.id = IpFile.name = '_' + prefix + '_IpFile_' + Id++;
    IpFile.onchange = function () { Add(DivFiles, DivListBox, hdnobj, prefix); }
    IpFile.type = 'file';
    return IpFile;
}


function CreateItem(IpFile, DivFiles, DivListBox, hdnobj, prefix) {
    Id = GetTotalItems(DivListBox) + 1;

    var MAX = 1;

    if (hdnobj != null)
        MAX = hdnobj.value;

    var Item = document.createElement('div');
    Item.className = "file-item";
    var Splits = IpFile.value.split('\\');
    Item.innerHTML = Splits[Splits.length - 1] + '&nbsp;';
    Item.value = IpFile.id;
    Item.title = IpFile.value;
    var A = document.createElement('a');
    A.innerHTML = 'Delete';
    A.id = 'A_' + Id;
    A.href = 'javascript:void(0);';
    A.style.color = 'blue';
    A.onclick = function () {
        DivFiles.removeChild(document.getElementById(this.parentNode.value));
        DivListBox.removeChild(this.parentNode);
        if (MAX != 0 && GetTotalFiles(DivFiles) - 1 < MAX) {
            GetTopFile(DivFiles).disabled = false;
        }
        return false;
    }



    A.onmouseover = function () {
        A.bgColor = A.style.backgroundColor;
        A.fColor = A.style.color;
        A.style.color = '#ffffff';
        //A.style.fontWeight = 'bold';
    }
    A.onmouseout = function () {
        A.style.backgroundColor = A.bgColor;
        A.style.color = A.fColor;
        //A.style.fontWeight = 'normal';
    }

    Item.appendChild(A);

    //        var lblcopy =  document.createElement('label');
    //        lblcopy.innerHTML = "Copyright:"
    //        Item.appendChild(lblcopy);
    //         
    //        var txtbox = document.createElement('input');
    //        txtbox.id = 'txtCopyright_' + Id;
    //        Item.appendChild(txtbox);
    //        
    //        var clrfix = document.createElement('div');
    //        clrfix.className = 'clearfix';
    //        Item.appendChild(clrfix);

    return Item;
}

function Clear(DivFiles, DivListBox, hdnobj, prefix) {
    DivListBox.innerHTML = '';
    DivFiles.innerHTML = '';
    DivFiles.appendChild(CreateFile(DivFiles, DivListBox, hdnobj, prefix));
}

function GetTopFile(DivFiles) {
    var Inputs = DivFiles.getElementsByTagName('input');
    var IpFile = null;
    for (var n = 0; n < Inputs.length && Inputs[n].type == 'file'; ++n) {
        IpFile = Inputs[n];
        break;
    }
    return IpFile;
}

function GetTotalFiles(DivFiles) {
    var Inputs = DivFiles.getElementsByTagName('input');
    var Counter = 0;
    for (var n = 0; n < Inputs.length && Inputs[n].type == 'file'; ++n)
        Counter++;

    return Counter;
}

function GetTotalItems(DivListBox) {
    var Items = DivListBox.getElementsByTagName('div');
    return Items.length;
}

function DisableTop(DivFiles) {
    if (GetTotalItems(DivFiles) == 0) {
        alert('Please browse at least one file to upload.');
        return false;
    }

    GetTopFile(DivFiles).disabled = true;
    return true;
}

function UpdateMax(DivFiles, DivListBox, ddlobj, hdnobj) {
    //alert("UpdateMax");

    // check if reduction available
    var Totalfiles = GetTotalItems(DivListBox);
    var TopFile = GetTopFile(DivFiles);

    if (Totalfiles > ddlobj.value) {
        alert("You currently have " + Totalfiles + " files selected, you will need to delete " + (Totalfiles - ddlobj.value) + " if you want to only upload " + ddlobj.value);
        ddlobj.value = Totalfiles;
        TopFile.disabled = true;
    }
    else if (Totalfiles < ddlobj.value) {
        TopFile.disabled = false;
    }
    else
        TopFile.disabled = true;

    // assign selected count
    hdnobj.value = ddlobj.value;
}
// END MULTI FILE UPLOAD


function getObj(name) {
    if (document.getElementById) {
        this.obj = document.getElementById(name);
        if (this.obj) this.style = document.getElementById(name).style;
    } else if (document.all) {
        this.obj = document.all[name];
        if (this.obj) this.style = document.all[name].style;
    } else if (document.layers) {
        if (document.layers[name]) {
            this.obj = document.layers[name];
            if (this.obj) this.style = document.layers[name];
        } else {
            this.obj = eval("document.layers." + name + ".layers[name]");
            if (this.obj) this.style = eval("document.layers." + name + ".layers[name]");
        }
    }
}


function showdiv(objid, bool) {
    obj = document.getElementById(objid);

    if (bool)
        obj.style.display = "";
    else
        obj.style.display = "none";

}

function printAdd(advertId) {
    //    var printCss = "<style media=\"print\"  type=\"text/css\"> ";

    //    $("body").find("div.advert-wrapper").each(function() {
    //        if ($(this).attr("id") != advertId) {
    //            printCss = printCss + "#" + $(this).attr("id") + "{display:none;} ";
    //        }
    //        else {
    //            printCss = printCss + "#" + $(this).attr("id") + "{display:block;} ";
    //        }
    //    });
    //    printCss = printCss + " </style>";

    //    $('body').append(printCss);

    //    //$('#innerPrintDiv').html(printCss);
    //    //$('#' + advertId).css('page-break-before', 'avoid');
    //    
    //    $('#' + advertId).removeClass('pagebreak');

    //    window.print();

    document.getElementById("ifPrnt").src = "/advertisment.aspx?id=" + advertId + "&print=1";

}




//function printAdd(advertId) {

//    $("body").find("div.advert-wrapper").each(function() {
//        if ($(this).attr("id") != advertId) {
//            $(this).addClass('noprint');

//        }
//    });

//    $('#' + advertId).css('page-break-before', 'avoid');
//    $('#' + advertId).removeClass('noprint');

//    window.print();
//}

function printDiv() {

    window.print();

}


function previewAdvert(id) {
    $(document).ready(function () {
        $("#adPreview").colorbox({
            open: true,
            title: "Preview Advert",
            onComplete: function () { $(this).colorbox.resize(); },
            onOpen: function () { $(this).colorbox({ href: "/members/popPreview.aspx?adId=" + id }) }
        });
    });
}

//SelectRegion = function (region) {
//    $("." + region).class("{fillColor:'ffffff'}");
//}

initInfo = function (e) {

    $(".maincontent").delegate("span[data-role='map']", "click", function () {
        $(this).colorbox({ href: "/images/regions.jpg" })
    });
}

initFakeSelectMenu = function () {
    $(".dropdown").each(function () {

        var $this = $(this);

        $this.mouseenter(function () {
            $this.find("dd ul").show();
        }).mouseleave(function () {
            $this.find("dd ul").hide();
        });
    });
}

initAutoSuggest = function () {
    $.ajax({
        type: "POST",
        url: "/service/hq.asmx/GetRegionList",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {



            $("[data-role='autosuggest']").each(function () {
                var $this = $(this);
                var preFillValues = $this.val();
                $this.autoSuggest(data.d, { selectedItemProp: "_name", searchObjProps: "_name", selectedValuesProp: "_id", preFill: preFillValues, startText: "Any - Nationwide" });
            });

            $("[data-role='submit']").click(function () {
                $(".as-selections").each(function () {
                    var $this = $(this);
                    var $hidden = $this.siblings("[id*='hdnStateIds']");
                    $hidden.val($this.find(".as-values").val());
                });
            });
        }
    });
}

initAdvancedLink = function () {
    $("[data-role='adv-search']").click(function (e) {
        e.preventDefault();

        var $this = $(this);

        if ($this.text() == "Simple Search >>>") {
            $("[data-id='adv-search']").hide();
            $this.text("Advanced Search >>>");
        } else {
            $("[data-id='adv-search']").show();
            $this.text("Simple Search >>>");
        }
    });
}

initSearchForm = function () {
//    $("[data-id='SearchForm'] input").each(function (e) {

//        var $this = $(this);

//        if ($this.is(':text') && $this.val() == '') {
//            $this.val("Any").click(function () {
//                var $txt = $(this);
//                if ($txt.val() == "Any") {
//                    $txt.val("");
//                }
//            });
//        }
//    });
}

