var DDSPEED = 10;
var DDTIMER = 15;

var collapsed_menu_points = {};

function RedirectUrl(url) {
    location.href = url;
}

function AddToBasket(lang_key, product_id, quantity, divmsg){

    jQuery('.loader_'+product_id).fadeIn(1000);
    jQuery('#txtloadmsg').html(divmsg);
    var strQuantity = jQuery('#'+quantity+'_'+product_id)[0].value;

    jQuery.get('/'+lang_key+'_b/add/'+product_id+'/'+strQuantity+'/', function(data) {
        
            jQuery('.loader_'+product_id).fadeOut(2000);
            jQuery('.txtloader_'+product_id).fadeIn(1000, function() {
                setTimeout(function() {
                    jQuery('.txtloader_'+product_id).fadeOut(1000);
                },1500);
            });
            
        jQuery.get('/content/basket/ajax_update.php', function(update) {
            jQuery('#ajaxMiniBasket').html(update);
        });
        
        }, 'html');
}

function WishlistRemove(product_id, category_id) {
    
    jQuery('.loader_'+product_id).fadeIn(300);
    jQuery('.favIcon_'+product_id).fadeOut(200);
    /* Executes the wishlist delete function*/
    jQuery.get('/content/wishlist/del.php?productid='+product_id, function(data) {

        /* Handles the fadeout's, new picture of checked button and new onclick function */
        jQuery('.loader_'+product_id).fadeOut(300);
        jQuery('.favIcon_'+product_id)[0].src = "/style/graphics/fav-unchecked.png";
        jQuery('.favLink_'+product_id).unbind('click');
        jQuery('#product_'+product_id).unbind('click');
        jQuery('.favLink_'+product_id).bind('click', function(runit){ AddToWishList(product_id, category_id); });
        jQuery('.favIcon_'+product_id).fadeIn(1000);
        jQuery('#product_'+product_id).fadeOut(1000);

    }, 'html');
    
}

function RemoveFromWishlist(product_id, category_id) {
    
    jQuery('.loader_'+product_id).fadeIn(300);
    jQuery('.favIcon_'+product_id).fadeOut(200);
    /* Executes the wishlist delete function*/
    jQuery.get('/content/wishlist/del.php?productid='+product_id, function(data) {

        /* Handles the fadeout's, new picture of checked button and new onclick function */
        jQuery('.loader_'+product_id).fadeOut(300);
        jQuery('.favIcon_'+product_id)[0].src = "/style/graphics/fav-unchecked.png";
        jQuery('.favLink_'+product_id).unbind('click');
        jQuery('#product_'+product_id).unbind('click');
        jQuery('.favLink_'+product_id).bind('click', function(runit){ AddToWishList(product_id, category_id); });
        jQuery('.favIcon_'+product_id).fadeIn(1000);

    }, 'html');
    
}

function AddToWishList(product_id, category_id, divmsg){

    jQuery('.loader_'+product_id).fadeIn(300);
    jQuery('.favIcon_'+product_id).fadeOut(200);
    jQuery('#txtloadmsg').html(divmsg);
    jQuery.get('/content/wishlist/add.php?productid='+product_id+'&categoryid='+category_id, function(data) {
        jQuery('.loader_'+product_id).fadeOut(300);
        jQuery('.favLink_'+product_id).unbind('click');
        jQuery('.favLink_'+product_id).bind('click', function(runit){ RemoveFromWishlist(product_id, category_id); });
        jQuery('.favIcon_'+product_id)[0].src = "/style/graphics/fav-checked.png";
        jQuery('.favIcon_'+product_id).fadeIn(1000);
    }, 'html');
}

function CheckIfNull(input)
{
    var value;
    if (jQuery('#' + input)[0].value == '') {
        value = ' ';
    } else {
        value = jQuery('#' + input)[0].value;
    }
    return value;
}

function CheckAndAddAntal(){

    var BasketCount = document.getElementById('products').value;
    document.getElementById('AddToBasketButton').href += BasketCount + '/';
}


function ValidateInput(input, error_message, error_div, regex)
{

    var priceregex=regex;
    if (jQuery('#' + input)[0].value.search(priceregex)==-1)
    {
         jQuery('#' + error_div).append(error_message);
         return false;
    } else {
         jQuery('#' + error_div).value = "";
         return true;
    }
}

function SearchRedirect(input)
{
    location.href = "/search_da/" + input + "";
}

function ReplaceNSearch(string, langid, error_msg)
{

    var send_data = { 'items': '' };
    var items = new Array();
    
    newstring = new Array(string);
    newstring.join(";;");
    
    KeyWords = document.getElementById(newstring).value;
    KeyWords = KeyWords.replace(/^\s+|\s+$/g,"");

    var length = KeyWords.length;
    if (length < 3) {
        alert(error_msg);
    } else {
        KeyWords = escape(KeyWords);
        KeyWords = KeyWords.replace(/\//g, '%2F');
        location.href = "/" + langid + "_k/"+ escape(KeyWords) + '/';
    }
}

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

// collapse the menu //
function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}

function menu_collapse(id)
{
    if(id in collapsed_menu_points || jQuery('#'+id+' ul').length == 0) {
        return true;
    } else {
        jQuery('#'+id+' ul').slideUp(750);
        collapsed_menu_points[id] = 1;
        return false;
    }
}
