$(function () {
    //ajax setup
    $.ajaxSetup({ cache: false, dataType: "html" });

});

//document click close div
function documentClickClose(classNameHandle, classNameClose, additionalExcetuteCode) {
    //hide on click
    $(document).click(function () {
        $("." + classNameClose).hide();
        if (additionalExcetuteCode != "") eval(additionalExcetuteCode);
    });
    //return false
    $("." + classNameHandle).click(function () {
        return false;
    });
}

//close div
function closeHeaderDiv() {
    $('.linkmoreselected').addClass('linkmore').removeClass('linkmoreselected');
}

//set ajax newsletter
function setAjaxNewsletter() {
    if ($('#newsletteremail').hasClass('newsletteremailnormal')) {
        $.ajax({
            url: 'http://www.ciciklub.si/ajaxnewsletter.aspx',
            data: ({ email: $('#newsletteremail').val() }),
            success: function (data) {
                //errors / no errors
                if (data.split('###')[0] == '1') {
                    $('#newslettererrors').html(data.split('###')[1]);
                    $('#newsletteremailmain').attr('style', 'border:1px #f5150d solid;');
                }
                else {
                    $('#newslettererrors').html(data.split('###')[1]);
                    $('#newsletteremailmain').attr('style', 'border:1px #000000 solid;');
                }
            }
        });
    }
}

//open / hide div
function openHeaderDiv(id) {
    if ($('#linkmore' + id).is(':visible')) {
        $('#linkmore' + id).hide();
        $('#mainlink' + id).addClass('linkmore').removeClass('linkmoreselected');
    }
    else {
        $('.linkmorelinks').hide();
        $('.linkmoreselected').removeClass('linkmoreselected').addClass('linkmore');
        $('#linkmore' + id).show();
        $('#mainlink' + id).addClass('linkmoreselected').removeClass('linkmore');
    }
}

//key: only numeric 
function keyOnlyNumericWEnter(e) {
    //validate input
    if (navigator.appName == "Netscape")
        var keyIndex = e.which; //for netscape
    else
        var keyIndex = window.event.keyCode; //other

    //only allow numbers and backspace
    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 46 || keyIndex == 44 || keyIndex == 13)
        return true;
    else
        return false;
}

//ajax order
function ajaxOrder() {
    $.ajax({
        url: 'http://www.ciciklub.si/ajaxquickorder.aspx',
        data: ({ quantity: $('#headerquantity').val(), ident: $('#headerident').val() }),
        success: function (data) {
            $('#headerquantity').removeAttr('style');
            $('.redarrow').removeAttr('style');

            if (data == '') {
                $('#headerquickorderform').submit();
            }
            else {
                $('#headerquickordererror').show().html(data);
                if (parseInt($('#headerquantity').val()) == 0 || $('#headerquantity').val() == 'Kol.')
                    $('#headerquantity').attr('style', 'border:1px #f5150d solid;');
                else
                    $('.redarrow').attr('style', 'border:1px #f5150d solid;');
            }
        }
    });
}

//ajax basket order
function ajaxBasketOrder() {
    $.ajax({
        url: 'http://www.ciciklub.si/ajaxquickorder.aspx',
        data: ({ quantity: $('#basketquantity').val(), ident: $('#basketident').val() }),
        success: function (data) {
            $('#basketquantity').removeAttr('style');
            $('#redarrowbasket').removeAttr('style');
            if (data == '') {
                $('#basketquickorderform').submit();
            }
            else {
                $('#basketquickordererror').show().html(data);
                if (parseInt($('#basketquantity').val()) == 0)
                    $('#basketquantity').attr('style', 'border:1px #f5150d solid;');
                else
                    $('#redarrowbasket').attr('style', 'border:1px #f5150d solid;');
            }
        }
    });
}

//submit form on enter pressed
function submitOnEnterAndExec(e, exec) {

    if (e && e.which) {
        //character code is contained in NN4's which property
        e = e
        characterCode = e.which
    }
    else {
        //character code is contained in IE's keyCode property
        e = event
        characterCode = e.keyCode
    }
    //if generated character code is equal to ascii 13
    if (characterCode == 13) {
        //submit the form
        eval(exec);
        return false
    }
    else {
        return true
    }
}

// show popup on cnange password click
function ajaxChangePassword(validate) {

    if (validate == 0) {
        //set window width and height
        $('#modalwindow').css({ width: 596, height: 350 });
        $('#modalwindow').addClass('hiddenpopupforlogin');
    }

    $.ajax({
        url: 'http://www.ciciklub.si/ajaxchangepassword.aspx',
        data: ({ email: $('#email').val(), validate: validate, oldpassword: $('#oldpassword').val(), newpassword: $('#newpassword').val(), repeatnewpassword: $('#repeatnewpassword').val() }),
        success: function (data) {
            $('#modalwindow').html(data);
        }
    });

    if (validate == 0) {
        //show popup
        openPopup();
    }
 }

// show popup on forgotten password click
function ajaxSendPassword(validate) {

    if (validate == 0) {
        //set window width and height
        $('#modalwindow').css({ width: 420, height: 200 });
        $('#modalwindow').addClass('hiddenpopupforlogin');
    }

    $.ajax({
        url: 'http://www.ciciklub.si/ajaxsendpassword.aspx',
        data: ({ email: $('#email').val(), validate: validate }),
        success: function (data) {
            $('#modalwindow').html(data);
        }
    });

    if (validate == 0) {
        //show popup
        openPopup();
    }
 }

/* POPUP */

//0 = disabled; 1 = enabled;  
var popupStatus = 0;

//show popup
function openPopup() {

    if (popupStatus == 0) {
        //set that popup is open
        popupStatus = 1;

        //check for IE - show modal window
        if ($.browser.msie) {
            $('#modalwindowbackground').css({ 'opacity': '0.0' });
            $('#modalwindowbackground').css('display', 'block');
            $('#modalwindow').css('display', 'block');
        }
        else {
            $('#modalwindowbackground').css({ 'opacity': '0.0' });
            $('#modalwindowbackground').fadeIn('fast');
            $('#modalwindow').fadeIn('fast');
        }

        //popup - center
        centerPopup();
    }
}

//position popup in center of window
function centerPopup() {
    //request data for centering
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();

    var popupWidth = $("#modalwindow").width();
    var popupHeight = $("#modalwindow").height();

    var newWidth = windowHeight / 2 - popupHeight / 2;
    var newHeight = windowWidth / 2 - popupWidth / 2;

    //centering
    $('#modalwindow').css({ top: newWidth - 100, left: newHeight });
}

//dispose popup
function disposePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        //mozilla
        if ($.browser == "mozilla" && $.browser.version.substr(0, 3) == "1.9") {
            $('#modalwindowbackground').fadeOut('fast');
            $('#modalwindow').fadeOut('fast');
            $('#adseldiv').fadeOut('fast');
        } else {
            $('#modalwindowbackground').css('display', 'none');
            $('#modalwindow').css('display', 'none');
            $('#adseldiv').css('display', 'none');
        }

        //set that popup is closed
        popupStatus = 0;
    }
}

//close popup window
function closePopup() {
    //popup - dispose
    disposePopup();
}


/* CONTENT */
function openNode(nodeid) {
    $('#childrenof' + nodeid.toString()).toggle("slow", function () {
        $('#childrenof' + nodeid.toString()).css("opacity", "1"); 
    });
   
}

// show content image popup
function showAjaxLargeImage(position, docid, articleid) {

    //set window width and height
    $('#modalwindow').css({ width: 530, height: 510 });
    $('#modalwindow').addClass('modalwindowlargeimage');

    //load article image
    $('#modalwindow').html($('#popuplargeimage').html());

    $.ajax({
        url: 'http://www.ciciklub.si/ajaxlargeimage.aspx',
        data: ({ position: position, docid: docid, articleid:articleid }),
        success: function (data) {
            $('#modalwindow').html(data);
        }
    });

    //show image
    openPopup();
}


/* toolbar */
function bookmark(title) {
    var url = document.location; //windowdow.location.href ;

    if (window.sidebar) window.sidebar.addPanel(title, url, "");

    if (window.opera && window.print) {
        var mbm = document.createElement('a');
        mbm.setAttribute('rel', 'sidebar');
        mbm.setAttribute('href', url);
        mbm.setAttribute('title', title);
        mbm.click();
    }
    else if (document.all) {
        window.external.AddFavorite(url, title) 
     };
}

//share on facebook or twitter
function bookmark_ft(type) {
    var u = location.href; //get current browser url
    var t = document.title; //get current browser title
    var url = '';

    if (type == 'f') {
        url = 'http://www.facebook.com/sharer.php?u=';
    }
    if (type == 't') {
        url = 'http://twitter.com/home/?status=';
    }

    window.open(url + encodeURIComponent(u) + '&amp;t=' + encodeURIComponent(t), 'sharer');
    //return false; //halts href link
}

// font increase function
function fontIncrease(increase) {

    //get current font size
    var currentFontSize = $('.rightcontent .content').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = 0;

    //increase
    if (increase) {
        newFontSize = currentFontSizeNum * 1.2;
    }
    //decrease
    else {
        newFontSize = currentFontSizeNum * 0.8;
    }

    //execute
    if (newFontSize > 9 && newFontSize < 20 ) {
        $('.rightcontent .content').css('font-size', newFontSize);
    }
}

// split price values onChange
function selectPrice() {

    // get pricefrom and priceto
    var pricefrom = $('#price').val().split('-')[0];
    var priceto = $('#price').val().split('-')[1];
    
    // set values to hidden inputs
    $('#pricevaluefrom').val(pricefrom);
    $('#pricevalueto').val(priceto);
}

// split price values onChange
function selectSearchPrice() {

    // get pricefrom and priceto
    var pricefrom = $('#price').val().split('-')[0];
    var priceto = $('#price').val().split('-')[1];

    // set values to hidden inputs
    $('#pricevaluefrom').val(pricefrom);
    $('#pricevalueto').val(priceto);
    $('#sitesearchform').submit();
}

//set articles page
function setArticlesPage(page) {
    $('#page').val(page);
    $('#articlesform').submit();
}

//set search page
function setSearchPage(page) {
    $('#page').val(page);
    $('#sitesearchform').submit();
}

//set search page
function setSearchType(contenttypeid) {
    $('#contenttypeid').val(contenttypeid);
    $('#sitesearchform').submit();
}

//set articles recordsonpage
function setArticlesRecordsOnPage(recordsonpage) {
    $('#recordsonpage').val(recordsonpage);
    $('#page').val(1);
    $('#articlesform').submit();
}

//set articles sort
function setArticlesSort(sort) {
    $('#sort').val(sort);
    $('#page').val(1);
    $('#articlesform').submit();
}


function showAjaxArticleImage(position, docid, articleid, color, validate) {
        
    //call ajax
    $.ajax({
        url: 'http://www.ciciklub.si/ajaxlargeimage.aspx',
        data: ({ position: position, docid: docid, articleid: articleid, color: color, validate: validate }),
        success: function (data) {
            $('#tab5').html(data);
        }
    }); 
}

//select tab
function selectTab(tab, color, articleid) {

    //all tabs headers
    $(".tab").removeClass("selected").addClass(color);

    //all tabs contents
    $(".tabcontent").removeClass("selected");

    //selected tab header
    $("#tabheader" + tab.toString()).addClass("selected").removeClass(color);

    //selected tab content
    $("#tab" + tab.toString()).addClass("selected");

    if (tab = 5)
        showAjaxArticleImage(1, 0, articleid, color, 2);
}

//articles scroller
function mycarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function () {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function () {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function () {
        carousel.stopAuto();
    }, function () {
        carousel.startAuto();
    });
};

// article - send to friend
function sendToFriend(contenttypeid, contentid, contenttitle) {

    //set window width and height
    $('#modalwindow').css({ width: 530, height: 530 });
    $('#modalwindow').addClass('modalwindowsendtofriend');

    $.ajax({
        url: 'http://www.ciciklub.si/ajaxsendtofriend.aspx',
        data: ({ contenttypeid:contenttypeid, contentid: contentid, contenttitle: contenttitle }),
        success: function (data) {
            $('#modalwindow').html(data);
        }
    });

    //open popup
    openPopup();
};

// article - send to friend submit
function sendToFriendSubmit() {
    var message = $('#message').val().replace(/%0A/g, '?newline?');
    //alert(message);
    $.ajax({
        url: 'http://www.ciciklub.si/ajaxsendtofriend.aspx',
        data: ({ contenttypeid: $('#contenttypeid').val(), contentid: $('#contentid').val(), contenttitle: $('#contenttitle').val(), recipientname: $('#recipientname').val(), recipientemail: $('#recipientemail').val(), sendername: $('#sendername').val(), senderemail: $('#senderemail').val(), message: message, equationvalue: $('#equationvalue').val(), equationresult: $('#equationresult').val(), validate: 1 }),
        success: function (data) {
            $('#modalwindow').html(data);
        }
    });
}

//removes article from basket
function RemoveFromBasket(id) {
    $('#articleid').val(id);
    $('#validate').val(1); //remove from basket
    $('#baskettype').val(1); //basket
    $('#basketform').submit();
}

//ORDERDELIVERYPAYMENT - delete search fields
function openDeleteSearchString() {
    $('.delivery .searchstring').val('');
    $('form#orderdeliverypaymentform').submit();
}

//ORDERDELIVERYPAYMENT - delete alternative delivery address
function deleteAddress(businesspartnerdeliveryaddressid) {
    $('#validate').val(2);
    $('#removebusinesspartnerdeliveryaddressid').val(businesspartnerdeliveryaddressid);
    $('form#orderdeliverypaymentform').submit();
}

//submit form
function submitChangePersonalData() {
    $('form#changpersonaldata').submit();
}

//order: show agree text
function showOrderAgreeText() {
    $('.agreetext').toggle('fast');
}


$(document).ready(function () {

    // content left menu open hide  submenu
    $('.contentholder').click(function () {
        if ($(this).next('.children').is(':visible')) {
            $(this).next('.children:visible').slideUp('slow');
        } else {
            $(this).next('.children').slideDown('slow');
        }
    });

});


//set article vote
function setArticleVote(validate, vote, articleid, color) {
    $.ajax({
        url: 'http://www.ciciklub.si/ajaxarticle.aspx',
        data: ({ vote: vote, validate: validate, articleid: articleid, color: color }),
        success: function (data) {
            $('#votediv').html(data);
        }
    });
}

//set article comment
function setArticleComment(validate, articleid, vote, comment, author, color) {
    $.ajax({
        url: 'http://www.ciciklub.si/ajaxarticle.aspx',
        data: ({ validate: validate, articleid: articleid, vote: vote, comment: comment, author: author, color: color }),
        success: function (data) {
            $('#votediv').html(data);
        }
    });
}


//set article comment
function addToBasket(articleid, quantity) {
    $.ajax({
        url: 'http://www.ciciklub.si/ajaxsmallbasket.aspx',
        data: ({ articleid: articleid, quantity: quantity }),
        success: function (data) {
            $('#ajaxsmallbasket').html(data.split('###')[1]);
            if (data.split('###')[0] != "0" && articleid > 0 && quantity > 0)
                scrollHeaderSmallBasketDiv();
        }
    });
}


