﻿
var shellWebServicePath = "/WebServices/ShellService.asmx";

$(document).ready(function () {
    preloadImages([
		'./../App_Themes/Elephant/images/tags_button_active.gif',
		'./../App_Themes/Elephant/images/menu_left_bg_active_first.png',
		'./../App_Themes/Elephant/images/menu_right_bg_active.png',
		'./../App_Themes/Elephant/images/menu_left_bg_active.png',
		'./../App_Themes/Elephant/images/menu_bg_active.png',
		'./../App_Themes/Elephant/images/top_links_marker_active.gif',
		'./../App_Themes/Elephant/images/bkg_button_active.png']);

    Cufon.replace('.h2-title, button.button, #nav > li > a, .breadcrumbs li, .block .block-title span, .page-title h1, .page-title h2, .product-view .short-description h2, .product-view .product-img-box .more-views h2, .box-collateral h2, .cart .discount h2, .cart .shipping h2');
    Cufon.replace('.home-banner-1 table tr td a', { hover: 'true' });

    //start plugins
    $('#divInformations').vTicker({
        speed: 500,
        pause: 3000,
        animation: 'fade',
        mousePause: false,
        showItems: 2
    });

    $('#nav li').hover(
        function () {
            $(this).addClass('over');
            $(this).find('a').addClass('over');

        },
        function () {
            $(this).removeClass('over');
            $(this).find('a').removeClass('over');

        }
    );

    $('#liProducts').hover(
        function () {
            $('#ulSubProducts').addClass('shown-sub');
            $('#ulSubProducts').width(210);
        },
        function () {
            $('#ulSubProducts').removeClass('shown-sub');
        }
        );

    setTimeout('SetControlFocuses()', 1000);
});

function SetControlFocuses() {
    //search
    $('input[id*=txtSearch]').focus(function () {
        ControlFocused(this, "Search ...")
    });
    $('input[id*=txtSearch]').blur(function () {
        ControlBlur(this, "Search ...")
    });

    //username
    $('input[id*=_UserName]').focus(function () {
        ControlFocused(this, "eMail:");
    });
    $('input[id*=_UserName]').blur(function () {
        ControlBlur(this, "eMail:");
    });

    //password
    $('input[id*=_Password]').focus(function () {
        ControlFocused(this, "password:");
    });
    $('input[id*=_Password]').blur(function () {
        ControlBlur(this, "password:");
    });
}

function AddToFavorites() {

    title = document.title;
    url = location.href

    if (window.sidebar) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title, url, "");
    } else if (window.external) { // IE Favorite
        window.external.AddFavorite(url, title);
    }
    else if (window.opera && window.print) { // Opera Hotlist
        return true;
    }
}

function ControlFocused(control, defaultText) {
    if (control.value == defaultText) {
        control.value = '';
        control.style.fontStyle = '';
        control.style.color = '#a1a1a1';
    }
}

function ControlBlur(control, defaultText) {
    if (control.value.replace(/(^\s*)|(\s*$)/g, '') == '') {
        control.value = defaultText;
        control.style.fontStyle = 'Italic';
        control.style.color = "#a1a1a1";
    }
}

function ValidateSearchQuery() {
    var txtSearch = $('input[id*=txtSearch]');

    if (txtSearch.length == 1) {
        if (txtSearch[0].value.length < 2) {
            alert('Duzina teksta za pretragu je minimum 2 karaktera!');
            return false;
        }
        else if (txtSearch[0].value == 'Search ...') {
            alert('Unesite tekst za pretragu!');
            return false;
        }
        else {
            return true;
        }
    }
}

//AUTOCOMPLETE FUNSTIONS
var isAutoCompleteInProgress = false;

$(document).ready(function () {

    //add div for autocomplete
    var div = document.createElement('div');
    $(div).attr("id", "divAutocomplete");
    $('body').append(div);

    //hide div when user click outside
    $("body").click(function () {
        $('#divAutocomplete').hide();
    });

    //attach event for searching
    $('#txtSearch').keyup(function () {
        
        if (isAutoCompleteInProgress)
            return;

        isAutoCompleteInProgress = true;
        $.get("/CategoriesArticles/AutoComplete.aspx?search=" + $('#txtSearch').attr('value'), function (response) {

            //set autocpmplate html
            $('#divAutocomplete').html(response);
            //positioning
            var position = $('#panelSearch').offset();
            $("#divAutocomplete").css({ "left": (position.left) + "px", "top": (position.top + $('#panelSearch').height()) + "px" });
            $('#divAutocomplete').show();
            isAutoCompleteInProgress = false;
        });
    });
});

function ShowPanelForgottenPassword() {
    $find('mpeForgottenPassword').show();
    $('input[id*=txtRecoveryEmail]').focus();
}

function SendForgottenPassword() {
    var email = $('input[id*=txtRecoveryEmail]').attr('value');
    var re = new RegExp(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);

    if (email != null && email.length != 0 && email.match(re)) {
        Sys.Net.WebServiceProxy.invoke(shellWebServicePath,
        "SendForgottenPassword", false,
        { "email": email },
        OnSendForgottenPasswordSucceeded, OnSendForgottenPasswordFailed, "User Context", 60000);
    }
    else {
        alert('Unesite validnu email adresu!');
    }
}

function OnSendForgottenPasswordSucceeded(result, eventArgs) {
    $find('mpeForgottenPassword').hide();
    $('input[id*=txtRecoveryEmail]').attr('value', '');
    alert(result);
}

function OnSendForgottenPasswordFailed(error) {
    alert(error._message);
    $find('mpeForgottenPassword').hide();
    $('input[id*=txtRecoveryEmail]').attr('value', '');
}

function QuitSendingForgottenPassword() {
    $find('mpeForgottenPassword').hide();
    $('input[id*=txtRecoveryEmail]').attr('value', '');
}

//validation function
function ValidateNumber(sender, args) {

    var re = new RegExp(/^[1-9]+[0-9]*$/);
    if (args.Value == null || args.Value.length == 0 || !args.Value.match(re)) {
        args.IsValid = false;
        alert("Unesite ispravnu vrednost.");
    }
}

function DisablePageValidators() {

    if ((typeof (Page_Validators) != "undefined") && (Page_Validators != null)) {
        var i;
        for (i = 0; i < Page_Validators.length; i++) {
            ValidatorEnable(Page_Validators[i], false);
        }
    }
}
