﻿
/// <reference path="jquery-1.2.6.js" />

$(document).ready(function() {

    InitAjax();

    $(".langSubMenu").hover(function() {
        if (!$(this).hasClass("logged_in")) {
            $(".langSubMenu #flags").show();
        }
    }, function() {
        $(".langSubMenu #flags").hide();
    });

    /// Making external links to target=_blank
    $("a[rel='external']").attr("target", "_blank");

    if ($("#OrderList img").length) {
        $("#OrderList").load(SITE_URL + ORDERLISTMANAGER_URL + '?epslanguage=' + LANGUAGE_BRANCH + '&oid=0', null, function() {
        });
    }

    $("#MyOrders .currentListOption input").click(function() {
        $("#OrderList").load(SITE_URL + ORDERLISTMANAGER_URL + '?epslanguage=' + LANGUAGE_BRANCH + '&oid=' + $(this).val(), null, function() {
        });
    });

    $('#OrderPageMain  #OrderCustomerInfoOpen').click(function() {
        $('#OrderPageMain div.CustomerInfo').show();
        $('#OrderPageMain div.CustomerInfoHidden').hide();
    });

    $('#OrderCustomerInfoClose').click(function() {
        $('#OrderPageMain div.CustomerInfo').hide();
        $('#OrderPageMain div.CustomerInfoHidden').show();
    });

    $(".archive .year").click(function() {
        if ($("ul.yearItems", this).hasClass("open")) {
            $("ul.yearItems", this).removeClass("open");
            $(this).removeClass("minus")
        }
        else {
            $(".archive ul.yearItems").removeClass("open");
            $(".archive .year").removeClass("minus");
            $("ul.yearItems", this).addClass("open");
            $(this).addClass("minus");
        }
    });
    /* Population of dropdowns for provider and partner search boxes */
    /*****************************************************************/
    if ($(".SearchBox .boxBody .CountryDropDown").length) {
        $.getJSON(SITE_URL + CITYLISTHANDLER_URL, { c: $(".SearchBox .boxBody .CountryDropDown").val(), lang: LANGUAGE_BRANCH }, function(data) {
            var options = '';
            for (var i = 0; i < data.length; i++) {
                options += '<option value="' + data[i].Val + '">' + data[i].Name + '</option>';
            }
            $(".SearchBox .boxBody .citySelectDropDown").html(options);
        });
    }

    if ($(".SearchBox .boxBody .CountryDropDown").length) {
        $.getJSON(SITE_URL + COUNTYLISTHANDLER_URL, { c: $(".SearchBox .boxBody .CountryDropDown").val(), lang: LANGUAGE_BRANCH }, function(data) {
            var options = '';
            for (var i = 0; i < data.length; i++) {
                options += '<option value="' + data[i].Val + '">' + data[i].Name + '</option>';
            }
            $(".SearchBox .boxBody .countySelectDropDown").html(options);
        });
    }


    if ($(".SearchBox .boxBody .ProviderCountryDropDown").length) {
        $.getJSON(SITE_URL + CITYLISTHANDLER_URL, { c: $(".SearchBox .boxBody .ProviderCountryDropDown").val(), lang: LANGUAGE_BRANCH, mode: "provider" }, function(data) {
            var options = '';
            for (var i = 0; i < data.length; i++) {
                options += '<option value="' + data[i].Val + '">' + data[i].Name + '</option>';
            }
            $(".SearchBox .boxBody .ProviderCitySelectDropDown").html(options);
        });
    }

    $(".SearchBox .boxBody .CountryDropDown").change(function() {
        $.getJSON(SITE_URL + CITYLISTHANDLER_URL, { c: $(this).val(), lang: LANGUAGE_BRANCH }, function(data) {
            var options = '';
            for (var i = 0; i < data.length; i++) {
                options += '<option value="' + data[i].Val + '">' + data[i].Name + '</option>';
            }
            $(".SearchBox .boxBody .citySelectDropDown").html(options);
        });
    });

    $(".SearchBox .boxBody .ProviderCountryDropDown").change(function() {
        $.getJSON(SITE_URL + CITYLISTHANDLER_URL, { c: $(this).val(), lang: LANGUAGE_BRANCH, mode: "provider" }, function(data) {
            var options = '';
            for (var i = 0; i < data.length; i++) {
                options += '<option value="' + data[i].Val + '">' + data[i].Name + '</option>';
            }
            $(".SearchBox .boxBody .ProviderCitySelectDropDown").html(options);
        });
    });

    $(".SearchBox .boxBody .PartnerSearchButton").click(function() {
        $.cookie("ListPageCookie", null, { path: '/' });
        var coutry = $(".CountryDropDown", $(this).parent().parent()).val();
        var city = $(".citySelectDropDown", $(this).parent().parent()).val();
        var county = $(".countySelectDropDown", $(this).parent().parent()).val();

        Load(SITE_URL + PARTNERLIST_PAGE_URL + '?country=' + coutry + '&city=' + city + '&county=' + county);
    });

    $(".SearchBox .boxBody .ProviderPartnerSearchButton").click(function() {
        $.cookie("ListPageCookie", null, { path: '/' });
        var coutry = $(".ProviderCountryDropDown", $(this).parent().parent()).val();
        var city = $(".ProviderCitySelectDropDown", $(this).parent().parent()).val();
        var county = $(".ProviderCountySelectDropDown", $(this).parent().parent()).val();

        Load(SITE_URL + PROVIDERLIST_PAGE_URL + '?country=' + coutry + '&city=' + city + '&county=' + county + "&mode=provider");
    });

    /* ******************************** */

    var activeSearch = null;

    $("#Topsearch input").keyup(
        function(event) {
            if ($(this).val().length == 0) {
                $('#searchResultViewer').hide();
                $('#Search div.searchMiddle').html('<img src=\"/Templates/Public/Images/misc/ajax-loader.gif\" class=\"SearchLoading\" />');
            }
            if ($(this).val().length > 2 || (event.keyCode == 13 && $(this).val().length > 0)) {

                $('#searchResultViewer').css('top', '60px');
                $('#searchResultViewer').css('left', ($('#Topsearch input').position().left) - 115 + 'px');
                $('#searchResultViewer').show();

                if (activeSearch != null) {
                    $('#Search div.searchMiddle').html('<img src=\"/Templates/Public/Images/misc/ajax-loader.gif\" class=\"SearchLoading\" />');
                    activeSearch.abort();
                }

                activeSearch = $.ajax({
                    type: 'GET',
                    url: siteUrl + 'Templates/Public/Data/Search.aspx',
                    data: 'epslanguage=' + epslanguage + '&Search=' + escape($("#Topsearch input").val()),
                    dataType: 'html',
                    success: function(data) {
                        $('#searchResultViewer').html($(data).find('#PageData > '));
                        $("#Search div.searchMiddle div.productItem").unbind("hover");
                        $("#Search div.searchMiddle div.productItem").hover(
                                function() {
                                    $(this).toggleClass('mouseover');
                                    $(this).children("h5").css("color", "#fff");
                                },
                                function() {
                                    $(this).toggleClass('mouseover');
                                    $(this).children("h5").css("color", "#e97c00");
                                });
                        $(document).pngFix();
                        activeSearch = null;

                    }
                });


            }

            return false;

        }
        );

    $("body").click(function() {
        $('#searchResultViewer').hide();
    });

});

function InitAjax() {
        $(document).pngFix();

        $('#Topmenu ul li a.notselected').hover(
            function() {
                $(this).css('background-position', '0px -23px');
            },
            function() {
                $(this).css('background-position', '0px 0px');
        });

        $('#Topsearch input').focus(
            function() {
                $('#Topsearch input').val('')
            });

            $('.Subscription .input').focus(
        function() {
            $('.Subscription .input').val('')
        });

        $('div.Productsmall').hover(
        function() {
            $(this).css('background-position', '0px 0px');
        },
        function() {
            $(this).css('background-position', '0px -64px');
        });

        $('.ProductListItem').hover(
        function() {
            $(this).css('background-color', '#F5A036');
        },
        function() {
            $('.ToolTipViewer').hide();
            $(this).css('background-color', '#FCFCFC');
        });

        LoadButtons();
    }

function LoadButtons() {

    $(".ReadMoreButton_sv, .DownloadSafetydata_sv, .DownloadManual_sv, .AddToCart_sv, .EmptyCart_sv, .Update_sv, .Cancel_sv, .SendOrder_sv, .SendPassword_sv, .Login_sv, .Send_sv, .Find_sv, .Search_sv, .MoreInfo_sv, .Uplosad_sv, .Back_sv, .Add_sv, .ContinueShop_sv, .DownloadImage_sv, .DownloadOrderPad_sv, .AddToFairCart_sv").hover(
    function() {
        $(this).css("background-position", "0px -25px");
    },
    function() {
        $(this).css("background-position", "0px 0px");
    });

    $(".ReadMoreButton_da, .DownloadSafetydata_da, .DownloadManual_da, .AddToCart_da, .EmptyCart_da, .Update_da, .Cancel_da, .SendOrder_da, .SendPassword_da, .Login_da, .Send_da, .Find_da, .Search_da, .MoreInfo_da, .Uplosad_da, .Back_da, .Add_da, .ContinueShop_da, .DownloadImage_da, .DownloadOrderPad_da, .AddToFairCart_da").hover(
    function() {
        $(this).css("background-position", "0px -25px");
    },
    function() {
        $(this).css("background-position", "0px 0px");
    });

    $(".ReadMoreButton_no, .DownloadSafetydata_no, .DownloadManual_no, .AddToCart_no, .EmptyCart_no, .Update_no, .Cancel_no, .SendOrder_no, .SendPassword_no, .Login_no, .Send_no, .Find_no, .Search_no, .MoreInfo_no, .Uplosad_no, .Back_no, .Add_no, .ContinueShop_no, .DownloadImage_no, .DownloadOrderPad_no, .AddToFairCart_no").hover(
    function() {
        $(this).css("background-position", "0px -25px");
    },
    function() {
        $(this).css("background-position", "0px 0px");
    });

    $(".CloseButton_sv, .CloseButton_da, .CloseButton_no").hover(
    function() {
        $(this).css("background-position", "0px -21px");
    },
    function() {
        $(this).css("background-position", "0px 0px");
    });

    $(".ViewProductImage").hover(
    function() {
        $(this).css("background-position", "0px -17px");
    },
    function() {
        $(this).css("background-position", "0px 0px");
    });
}

function Load(url) 
{
    location.href = url;
}

function ToolTip(o, text) {
    
    if ($('div.ToolTipViewer').length == 0) {
        $('.footerArea').after('<div class=\"ToolTipViewer\"><div class=\"ToolTipViewerTop\"></div><div class=\"ToolTipViewerMidle\"></div><div class=\"ToolTipViewerBottom\"></div></div>');

        $('div.ToolTipViewer').mouseout(function() { $('div.ToolTipViewer').hide(); });
        $('div.ToolTipViewer').click(function() { $('div.ToolTipViewer').hide(); });
        $('body').click(function() { $('div.ToolTipViewer').hide(); });
        $(document).pngFix();
    }

    $('div.ToolTipViewerMidle').html(text);
    $('div.ToolTipViewer').css('top', ($(o).position().top - $('#ToolTipViewer').height() - 90) + 'px');
    $('div.ToolTipViewer').css('left', (parseInt($(o).position().left) + parseInt($(o).css('margin-left').replace('px', '')) - 15) + 'px');
    $('div.ToolTipViewer').show();
}

function PrintPage(id) {
    myRef = window.open(siteUrl + 'Templates/Public/Pages/Print.aspx?domId=' + id, 'mywin', 'left=20,top=20,width=560,height=500,toolbar=0,resizable=0');

}
