﻿

// General JS's used across the site
var isLettoPage = false;

// extend jQuery ajax with the capability of remembering the count of active ajax requests
$.activeAjaxRequestCount = 0;
 
$().ajaxSend(function() {
  $.activeAjaxRequestCount++;
  // register lazily to make sure it's the last one and get executed after all other handlers
  if (!$._ajaxErrorHandlerAdded) {
    $().ajaxError(function() {
      $.activeAjaxRequestCount--;
    });
    $._ajaxErrorHandlerAdded = true;
  }
})
.ajaxSuccess(function() {
  $.activeAjaxRequestCount--;
});

function addJsTagToPaymentLink() {
    $("a[href*='payment.aspx?']").each(function() { 
        var link = $(this).attr('href') + "&" + "js=1";
        $(this).attr('href',link); 
    });
    
    $("a[href*='payment.aspx']:not([href*='payment.aspx?'])").each(function(){
        var link = $(this).attr('href') + "?" + "js=1";
        $(this).attr('href',link); 
    });
}

/* Folding menu*/
/***************/
function foldOutMenu(menu) {
    var id = getMenuId($(menu))
    if ($(menu).children(".links").is(":hidden")) {
        $(menu).children(".links").css({'background-color': '#fcf5dc'}).slideDown(300);
        $(menu).children(".header").children("a").addClass('expanded');
        $.cookie(id, "true");
    } else {
        $(menu).children(".links").slideUp(300);
        $(menu).children(".header").children("a").removeClass('expanded');
        $.cookie(id, null);
    }
}

$('.foldMenu .header')
.hover(function () {
    $(this) //hover in
        .addClass('pretty-hover');
//        .css({'color': '#E50000'});
}, function () {
    $(this) //hover out
        .removeClass('pretty-hover');
//        .css({'color': '#3B382D'}); 
});

$("div#sidebar div.sidebar_section ul.sidebar_linklist li.foldMenu div.links").parent().children("div.header").click(function() {
	var a = $(this).children("a");
	$("div#sidebar div.sidebar_section ul.sidebar_linklist li.foldMenu div.links").parent().children("div.header").children("a").each(function() {
	    var b = $(this);
        var menu = b.parent().parent();
	    if(a != b && menu.children("div.links").is(":hidden") == false && b.attr("lang") == "False") foldOutMenu(menu);
	});
	foldOutMenu(a.parent().parent());
	return false; 
});

$('.foldMenu .links:not(div#sidebar div.sidebar_section ul.sidebar_linklist li.foldMenu div.links)').parent().children('.header').click(function () {
    foldOutMenu($(this).parent());
    return false;
});

$("div#sidebar div.sidebar_section ul.sidebar_linklist li.foldMenu").each(function() {
	if($(this).children("div.links").length == 0) 
	{
		$(this).children("div.header").click(function() {
			$("div#sidebar div.sidebar_section ul.sidebar_linklist li.foldMenu div.links").parent().children("div.header").children("a").each(function() {
				$.cookie($(this).attr("id") , null);
			}); 
						
		}); 
	}
});

 $('li.foldMenu').each(function() {
    initFoldMenu($(this));
  });
 
 $.cookie("js", "true"); 

function hideMenu(menu) {
  $(menu).children(".links").hide();
  $(menu).children(".header").children("a").removeClass('expanded');
}

function getMenuId(menu) {
     return $(menu).children(".header").children("a").attr('id');
}

function initFoldMenu(menu) {
   if ($.cookie("js") == null)
    { 
        var l = $(menu).children("div.links").children("ul");
        var h = $(menu).children("div.header"); 
        if ($(l).length == 0) return false;
        else
        {
            var id = getMenuId($(menu))
            if (initFoldMenu($(l)) == false && $(h).children("a[lang='True'].expanded").length == 0 && $(l).children("li").children("a[href='" + window.location + "']").length == 0) 
            {
                if($.cookie(id) == null) hideMenu($(menu));
                return false;
            }
            else 
            {
                $.cookie(id, "true");
                return true;
            }
        }
    }
    else return true;
}

//Remove html tags from quicksearch when search button is clicked
$('.searchButton').click(function () {
    var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
    $('.sidebar_searchfield').val($('.sidebar_searchfield').val().replace(regexp, ""));
});

// Unobtrusive popup for library posts
$('A[rel="librarypostpopup"]').click(function () {
    popWindow($(this).attr('href'), 700, 500);
    return false;
});


// Popup text above flags in header menu
$('.bubble').each(function () {
    var trigger = $('.trigger', this);
    var popup = $('.popup', this);
    $(trigger.get(0)).mouseover(function () {
        popup.css({
            top: -24,
            right: 5,
            display: 'inline-block',
            zIndex: 50
        }).animate({
            top: -30,
            opacity: 1
        },
        200);
        return false;
    }).mouseout(function () {
        popup.css({
            zIndex: -10,
            opacity: 0,
            display: 'none'
        });
        return false;
    });
});



function getStock(isbn,page,optionalPath)
{
    /*
    switch(page)
    {
    case 'product':
        var path = ".product #productPriceAmountShipping li.shippingTime span.inStock";
        var fade = 0;
        break;
    case 'searchresult':
        var path = optionalPath;
        var fade = 100;
        break;
    default:
        return false;
    }
    
    $.ajax({
        url: "ajaxgetstock.aspx",
        dataType: 'json',
        type: 'GET',
        data: "isbn="+isbn,
        timeout: 5000,
        success:function(stock)
        {  
            if (stock.length > 0)
            {
                if (stock[0].free < 100)
                {
                    $(path).children('span.inStockMoreThan').remove();
                }
                $(path).children('span.inStockQuantity')
                       .prepend(stock[0].free + " ")
                       .wrap("<span class=\"stockValue\" />");
                $(path).fadeIn(fade);
            }
        }
    }); 
    */
}

function popWindow (strPage, strWidth, strHeight) { msgwin = window.open(strPage,'_blank','scrollbars=yes,toolbar=no,location=no,status=no,menubar=no,copyhistory=no,resizeable=no,width='+strWidth+',height='+strHeight);} 
function popWindowExt (strPage, strWidth, strHeight) { msgwin = window.open(strPage,'_blank','scrollbars=yes,toolbar=no,location=no,status=no,menubar=no,copyhistory=no,resizeable=yes,width='+strWidth+',height='+strHeight);}
function popWindowMenu (strPage, strWidth, strHeight) { msgwin = window.open(strPage,'_blank','scrollbars=yes,toolbar=no,location=no,status=no,menubar=yes,copyhistory=no,resizeable=no,width='+strWidth+',height='+strHeight);}
function Close() {window.close();}
function setFocus() { $('#quickvalueid').focus(); }

$(function () {
 
    // Add JS tag to payment link
    addJsTagToPaymentLink();
    
});

