﻿$(document).ready(function () {
    Cufon.replace(".cufon-replace", { fontFamily: 'Avenir' });
    Cufon.replace(".header_menu_list > li", { hover: true, hoverables: { li: true }, ignore: { ul: true }, textless: { li: true }, fontFamily: 'Avenir' });
    Cufon.replace(".cufon-replace-light", { fontFamily: 'Avenir Light' });
    var isSelected = true;
    styleSelects();

    styleMediaElements();

    initExpandableList();

    $(".select_box_option").live("mousedown", function (event) {
        selectValue($(this));
        closeSelect($(this).parents(".select-box-wrapper"));
        event.stopPropagation();
    });

    $(".select_box_option").live("mouseenter", function (event) {
        $('.select_box_option_active').removeClass('select_box_option_active');
        $(this).addClass('select_box_option_active');
    });

    $(".select-box-wrapper").live("click", function (event) {
        if (!event.isPropagationStopped()) {
            openSelect($(this));
            event.stopPropagation();
        }
    });

    $(".select_box_options").live("mousedown", function (event) {
        event.stopPropagation();
        
        var ua = navigator.userAgent;
        
        if ((navigator.userAgent.match(/ie/i) != null)) {
            isSelected = false;
        }
        return false;
    });

    //to hide all selectboxes
    $(document).click(function (event) {


        if (!event.isPropagationStopped()) {
            $(".select_box_options").css("display", "none");
            $(".select-box-wrapper").attr("isopen", "false");
        }
    });


    $(".select_input").live("keypress", function () {
        var value = $(this).val().toLowerCase();
        $(this).parent().find(".select_box_option").each(function () {
            if ($(this).html().toLowerCase().indexOf(value) == 0) {
                $(this).css("display", "block");

            } else {
                $(this).css("display", "none");
            }
        });
    });

    $(".select_input").live("blur", function (event) {
        
        if (isSelected) {
            var value = $(this).val().toLowerCase();
            if ($(this).parent().find('[optiontext="' + value + '"]').length == 1) {
                selectValue($(this).parent().find('[optiontext="' + value + '"]'));
            } else {
                selectValue($(this).parent().find('.select_box_option').first());
            }
            closeSelect($(this).parents(".select-box-wrapper"));
        }
    });

    $(".select_input").live("keydown", function (event) {
        if (event.keyCode == 38 || event.keyCode == 40) {
            var wrapper = $(this).parents(".select-box-wrapper");
            var selectid = $(wrapper).attr('for-select');
            var sel = $('#' + selectid);
            var currentVal = $(sel).val();
            var newOpt = null;

            $('.select_box_option_active').removeClass('select_box_option_active');

            // up
            if (event.keyCode == 38) {
                newOpt = $(wrapper).find('[optionvalue="' + currentVal + '"]').prev();
                if ($(newOpt).length == 0) {
                    newOpt = $(wrapper).find('.select_box_option').first();
                }
            }

            // down
            if (event.keyCode == 40) {
                newOpt = $(wrapper).find('[optionvalue="' + currentVal + '"]').next();
                if ($(newOpt).length == 0) {
                    newOpt = $(wrapper).find('.select_box_option').last();
                }
            }

            selectValue(newOpt, true);
            $(newOpt).addClass('select_box_option_active');

            $(this).focus();
        }
    });

    $("#hide-n-show-fields").click(function () {
        if ($(this).is(':checked')) {
            $(".enquiry-form-hidden").each(function () {
                $(this).addClass("enquiry-form-visible");
                $(this).removeClass("enquiry-form-hidden");
            });
        } else {
            $(".enquiry-form-visible").each(function () {
                $(this).addClass("enquiry-form-hidden");
                $(this).removeClass("enquiry-form-visible");
            });

        }
    });

});

function selectValue(selectOption, stopChange) {
    var selectid = "#" + selectOption.parent().attr("selectboxid");
    $(selectid).children("option").removeAttr("selected");
    $(selectid).children('[value="' + selectOption.attr("optionvalue") + '"]').attr("selected", "selected");
    selectOption.parents(".select-box-wrapper").children("input").val(selectOption.html());
    selectOption.parent().find(".select_box_option").each(function () {
            $(this).css("display", "block");
        });

    if (stopChange != true) {
        $(selectid).change();
    }
}

function openSelect(selected) {
    $(".select-box-wrapper").each(function () {//close all others
        $(this).attr("isopen", "false");
        $(this).children(".select_box_options").css("display", "none");
    });
    selected.children(".select_box_options").css("display", "block");
    selected.attr("isopen", "true");

    $('.select_box_option_active').removeClass('select_box_option_active');
    var select = $('#' + $(selected).attr('for-select'));
    $('.select_box_option_active').removeClass('select_box_option_active');
    if (select.val() != '') {
        $(selected).find('[optionvalue="' + select.val() + '"]').addClass('select_box_option_active');
    }
    
    $(selected).find('input').focus();
}

function closeSelect(selected) {
    selected.attr("isopen", "false");
    selected.children(".select_box_options").css("display", "none");
}

/**
*   For a select to be styled it needs to have class select-orig and an id attribute
*/
function styleSelects() {
    var ua = navigator.userAgent;
    var isiPad = false
    if ((navigator.userAgent.match(/iPad/i) != null) || (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPod") != -1)) {
        isiPad = true;
    }
    if(isiPad){
        return false;
    }
    $(".select_box_option").remove();
    $(".select-box-wrapper").remove();

    $(".select-orig").each(function () {
        var selectid = $(this).attr("id");

        var selectedText = $(this).children("option:selected").text();

        var selectbody = '<div class="select-box-wrapper" for-select="' + selectid + '"><input id="val_' + selectid + '" type="text" class="select_input" value="' + selectedText + '" /><div class="select_box_options" selectboxid="' + selectid + '">';

        var list = $(this).children("option").each(function () {
            selectbody = selectbody + '<div class="select_box_option" optionvalue="' + $(this).val() + '" optiontext="' + $(this).html().toLowerCase() + '">' + $(this).html() + '</div>';
        });
        selectbody = selectbody + '</div></div>';
        $(this).after(selectbody);
    });

    $(".select-box-wrapper").css("display", "inline-block");
    $(".select-orig").css("display", "none");   
}

/************************** Media Elements *************************************************/
function styleMediaElements() {
    $(".media_element").click(function () {
        $(".darken_background").show();

        if ("GalleryItem" == $(this).attr("mediatype")) {
            $.ajax({
                url: "/ContentItem_GalleryItem/Play",
                data: { id: $(this).attr("cmsid"),
                    elementId: $(this).attr("elementid")
                },
                success: function (html) {
                    $(".lightbox_content").html(html);
                },
                error: (function () { alert("error: could not open video"); })
            });

        }else if ("Video" == $(this).attr("mediatype")) {
            $.ajax({
                url: "/ContentItem_VideoItem/Play",
                data: { id: $(this).attr("cmsid") },
                success: function (html) {
                    $(".lightbox_content").html(html);

                },
                error: (function () { alert("error: could not open video"); })
            });
        } else if ("Image" == $(this).attr("mediatype")) {
            $.ajax({
                url: "/ContentItem_ImageItem/Play",
                data: { id: $(this).attr("cmsid") },
                success: function (html) {
                    $(".lightbox_content").html(html);

                },
                error: (function () { alert("error: could not open image"); })
            });
        }
    });

    $(".embedded_gallery_navigation_next").live("click", function () {
        $.ajax({
            url: "/ContentItem_GalleryItem/PlayNext",
            data: { id: $(this).attr("galleryid"),
                currentId: $(this).attr("activeelementid")
            },
            success: function (html) {
                $(".lightbox_content").html(html);

            },
            error: (function () { alert("error: could not open gallery item"); })
        });
    });

    $(".embedded_gallery_navigation_previous").live("click", function () {
        $.ajax({
            url: "/ContentItem_GalleryItem/PlayPrevious",
            data: { id: $(this).attr("galleryid"),
                currentId: $(this).attr("activeelementid")
            },
            success: function (html) {
                $(".lightbox_content").html(html);

            },
            error: (function () { alert("error: could not open gallery item"); })
        });
    });

    $(".close_lightbox").live("click", function () {
        $(".lightbox_content").html("");
        $(".darken_background").hide();
        return false;
    });
}

/************************** Expandable list (sitemap) *********************************************/
function initExpandableList() {
    $(".list-icon").live('click', function () {
        if ($(this).hasClass("expandable-list-header-open")) { //its open
            $(this).removeClass("expandable-list-header-open");
            $(this).addClass("expandable-list-header-closed");
            $(this).parent("div").next("ul").hide('slow');
        } else {
            $(this).removeClass("expandable-list-header-closed");
            $(this).addClass("expandable-list-header-open");
            $(this).parent("div").next("ul").show('slow');
        }
        //            $(this).next().toggle('slow');
        //            return false;
        //        }).next().hide();

    });
    
}
