
function pageStart() {
    jQuery.noConflict();
    jQuery(document).ready(function () {
        RunOnPostBack();
        setupBrowser();
    }); 
}
function RunOnPostBack() {
    jQuery(document).pngFix();
    jQuery("a[rel^='prettyPhoto']").prettyPhoto();
    qTip();
    fadeImages();
}

var animationPanel;
function Begin(sender, args) {
    jQuery('input').blur();
    sender._postBackSettings.sourceElement.disabled = true 
    animationPanel = "." + sender._postBackSettings.sourceElement.getAttribute("rel") + "_animation_panel";
    jQuery(animationPanel).fadeIn('fast');
}
function End(sender, args) {
    animationPanel = "." + sender._postBackSettings.sourceElement.getAttribute("rel") + "_animation_panel";
    jQuery(animationPanel).fadeOut('fast');
    RunOnPostBack();
}
function HideqTip(Target) {
    jQuery(Target).qtip("hide")
}
function ie6() {
    if (navigator.userAgent.toLowerCase().indexOf("msie 6") !== -1) {
        return true
    }
    else {
        return false
    }
}
function qTip() {
    //jQuery('a[class*="qTip"]').each(function () {
    jQuery('*[class*="qTip"]').each(function () {
        if (typeof (jQuery(this).data('qtip')) === 'object') { return true; };
        var tooltip, target;
        if (jQuery(this)[0].className.split("qTip")[1].split(" ")[0].length > 0) {
            tooltip = getQTipReference("tooltip", jQuery(this)[0].className.split("qTip")[1].split(" ")[0]);
            target = getQTipReference("target", jQuery(this)[0].className.split("qTip")[1].split(" ")[0]);
        }
        jQuery(this).qtip(generateQtip(jQuery("span[rel*='" + this.id + "']").attr("innerHTML") == undefined ? jQuery(this).attr("title") : jQuery("span[rel*='" + this.id + "']").attr("innerHTML"), jQuery(this).attr("_width"), tooltip, target, jQuery(this).attr("_corner"), jQuery(this).attr("_solo"), jQuery(this).attr("_ready"), jQuery(this).attr("_x"), jQuery(this).attr("_y"), jQuery(this).attr("_location")));
    });
}

function getQTipReference(type, reference) {
    var references = ["Top", "Bottom", "Left", "Right", "TopRight", "BottomRight", "BottomLeft", "TopLeft"];
    var tooltips = ["bottomMiddle", "topMiddle", "rightMiddle", "leftMiddle", "bottomLeft", "topLeft", "topRight", "bottomRight"];
    var targets = ["topMiddle", "bottomMiddle", "leftMiddle", "rightMiddle", "topMiddle", "bottomMiddle", "bottomMiddle", "topMiddle"];
    for(i=0; i<references.length; i++){
        if (references[i] == reference) {
            if (type == "tooltip") {
                return tooltips[i];
            }
            else {
                return targets[i];
            }
        }
    }
    return undefined;
}

function generateQtip(content, width, tooltip, target, corner, solo, ready, adjustX, adjustY, location) {
    var _width = (width == undefined) ? 300 : parseInt(width);
    var _tooltip = (tooltip == undefined) ? defaultTooltip() : tooltip;
    var _target = (target == undefined) ? defaultTarget() : target;
    var _corner = (corner == undefined) ? _tooltip : corner;
    var _solo = (solo == false) ? false : true;
    var _adjustX = (adjustX == undefined) ? 0 : parseInt(adjustX);
    var _adjustY = (adjustY == undefined) ? 0 : parseInt(adjustY);
    var _location = (location == undefined) ? false : location;
    return { content: { text: content, prerender: true }, position: { target: _location, corner: { tooltip: _tooltip, target: _target }, adjust: { x: _adjustX, y: _adjustY} }, style: { width: _width, padding: 20, background: '#fff', color: '#8e8e8e', textAlign: 'center', border: { radius: 5, color: '#5cb1cf' }, filter: 'alpha(opacity=70)', opacity: 'opacity:0.7', tip: { corner: _corner, size: { x: 30, y: 30}} }, show: { solo: _solo, ready: false} }
}

function defaultTooltip() {
    if (ie6()) {
        return "rightMiddle";
    }
    else {
        return "bottomMiddle";
    }
}
function defaultTarget() {
    if (ie6()) {
        return "leftMiddle";
    }
    else {
        return "topMiddle";
    }
}
var currentPanel;
function stepNav(_currentPanel) {
    currentPanel = _currentPanel
    jQuery(".browser_navigation").children("li").children("a").each(function () {
        this.className = this.className.replace(" on", "");
    })
    try {
        jQuery(".browser_navigation").children("li").children("a")[currentPanel - 1].className += " on";
    }
    catch (err) {

    }
}
function stepClick(id, panelNumber) {
    stepNav(panelNumber);
    stepcarousel.stepTo(id, panelNumber);
    return false;
}
function setupBrowser() {

    jQuery("a[class*='arrow_']").click(function (e) {
        var selectedPanelNumber = parseInt(this.className.split("arrow_")[1].split(" ")[0]);
        if (this.className.indexOf(" on") !== -1) {
            return true;
        }
        else {
            stepClick("carousel", selectedPanelNumber)
            return false;
        }
    });

    // Setup Enquire Only
    jQuery(".contact_click").click(function (e) {
        this.className = this.className + " on";
        jQuery(this).qtip("hide");
        this.blur();
        stepcarousel.stepTo("carousel", 2);
    });

    // Setup Back Button
    jQuery(".enquire_back").click(function (e) {
        stepcarousel.stepTo("carousel", 1);
        jQuery(".contact_click").each(function(){
            this.className = this.className.replace(" on", "");
        });
    });

    //jQuery(".arrow_1").click();
    stepcarousel.stepTo("carousel", 1);
}

function EnquirePostBack() {
    setTimeout('stepcarousel.stepTo("carousel", 1)', 5000);
}

function authoriseDownload(DownloadPath) {
    jQuery.prettyPhoto.open(DownloadPath + "?iframe=true&width=350&height=447", 'Download Request', '');
}

function fadeImages() {
    // full fade
    jQuery(".fade").hover(function () {
        jQuery(this).find(".hide").each(function () {
            jQuery(this).dequeue();

            if (jQuery(this)[0].style.display == "inline") {
                jQuery(this).fadeTo("200", 1);
            }
            else {
                jQuery(this).fadeIn("200");
            };
        });
    },
    function () {
        jQuery(this).find(".hide").each(function () {
            jQuery(this).dequeue();
            jQuery(this).fadeTo("200", 0);
        });
    });

    // slight fade
    jQuery(".slight_fade").hover(function () {
        jQuery(this).find("img").each(function () {
            jQuery(this).dequeue();
            jQuery(this).fadeTo("500", 0.3)
        });
    },
    function () {
        jQuery(this).find("img").each(function () {
            jQuery(this).dequeue();
            jQuery(this).fadeTo("500", 1);
        });
    });

    // input border fade
//    jQuery(".contact_form").find("input").hover(function () {
//        jQuery(this).animate({ borderTop: '#5cb1cf' }, 500);
//    },
//    function () {
//        jQuery(this).animate({ borderTop: '#f2f2f2' }, 500);
//    });
}

function prepareForDownloadWindow(downloadBtn) {
    HideqTip(downloadBtn);
}
