/* set general behaviours */

$(document).ready(function(){

  $.history.init(ajax_load);
 
  fct_init_allpage();
  
	$("#cart").droppable({
	  hoverClass: 'hover1',
	  tolerance: 'touch',
	  over: function(event, ui) {
	    $("#cart_borders").addClass("cart_borders_over").removeClass("cart_borders");
    },
	  out: function(event, ui) {
	    $("#cart_borders").addClass("cart_borders").removeClass("cart_borders_over");
    },
	  
		drop: function(event, ui) {
	    $("#cart_borders").addClass("cart_borders").removeClass("cart_borders_over");
			add_prod(ui.helper.attr('id'),1,1);
		}
	});
  
  
  
  //$("#mcontent").sortable({handle: '#handle'});        

  /* panier buttons */
  $("#pbut_m").click(function () {
    $(this).toggleClass("ptd_m_open");
    fct_cart_toggle();
  });
  $("#pbut_r").click(function () {
    fct_cart_submit();
  });  
  $("#pbut_c").click(function () {
    go('iaccount.php?page=ac_com1');
  });
  
  
  $("#rcart3").click(function () {
    $("#rcart3_d").html('<img src="images/icon_loading.gif">');
    $("#rcart3_d").load('ajax_box_alist_save.php');
    $("#rcart3_d").dialog({ modal: true , width: 450, draggable: false, resizable: false});
    $("#rcart3_d").dialog('open');
  });     

  $("#rcart4").click(function () {
    $("#rcart4_d").html('<img src="images/icon_loading.gif">');
    $("#rcart4_d").load('ajax_box_alist_load.php');
    $("#rcart4_d").dialog({ modal: true , width: 450, draggable: false, resizable: false});
    $("#rcart4_d").dialog('open');
  }); 

  
  
  
});


/* end general behaviours */





/* ajax load main with history */

function ajax_load(hash) {
  
  $(document).scrollTop(0);
  $("#mcontent").html($('#pathbar').html()+""+$('#prod_loading_container').html());
  
  var fileName = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1).toLowerCase();
  var cat_open;
  
  
  // parse hash to get pid and cid
  var cpat=/[\b\-]?[CM]([0-9]+|PROMO|NEW|ALL)[\b\s\-.]/i;
  var cres = hash.match(cpat);
  if(cres)
    var cid = cres[1];

  var ppat=/[\b\-]?P([0-9]+)[\b\s\-.]/i;
  var pres = hash.match(ppat);
  if(pres)
    var pid = pres[1];

  var spat=/S:(.*)/i;
  var sres = hash.match(spat);
  if(sres)
    var str = sres[1];
  
  if(str){
     $("#mcontent").load("ep_search.php?str="+encodeURIComponent(str));
  }else if(fileName=="imyshop.php"){  
    if(pid){
      $("#mcontent").load("ep_fiche.php?pid="+pid+"&cid="+cid+"&t=myshop");
      cat_open=cid;
    }else if(cid){
      $("#mcontent").load("ep_myshop.php?cid="+cid);
        cat_open=cid;
    }else{
      $("#mcontent").load("ep_myshop.php");
    }
  }else{
    if(pid && cid){
      $("#mcontent").load("ep_fiche.php?pid="+pid+"&cid="+cid);
      cat_open=cid;
    }else if(cid && !pid){
      $("#mcontent").load("ep_browse.php?cid="+cid);
      var tpat=/[0-9]+/;
      if(tpat.test(cid))
        cat_open=cid;
    }else{
      $("#mcontent").load("ep_index.php");
    }  
  }	      


  $('#mcontent').ajaxComplete(function(evt, request, settings){
    fct_init_allpage();
    tree.closeAllItems(0);
    if(cat_open!=''){
      tree.openItem(cat_open);
      tree.selectItem(cat_open);
    }
    if($('#mcontent').html()=='') $('#mcontent').load('misc_pagenotfound.php');
  }); 

  return true;
}




function fct_init_allpage(){

  $("a[rel='history']").click(function(){
      $.history.load(this.href.substring(this.href.lastIndexOf('/') + 1));
      return false;
  });
  
  $("a[rel='history']").removeAttr("rel");
  

  // set lefttd height
  if(document.documentElement.clientHeight)
    $("#lefttd").height(document.documentElement.clientHeight-236);

  $(".prod_img,.prod_img_fiche,.prod_img_big").draggable({
    revert: true,
    revertDuration: 0,
    helper: 'clone',
    appendTo: 'body',
    start: function(event, ui) {
        ui.helper.css("z-index","20");
      }
   });  
   
   

}

function fct_get_cat_ajax(cid,myshop){
  if(myshop==1) l="M"; else l="C";
  if(tree.getUserData(cid,"url")){
    $.history.load(l+cid+'-'+tree.getItemText(cid)+'.html');
  }else{
    tree.closeAllItems(0);
    tree.openItem(cid);  
  }
}
function fct_get_cat_ajax_myshop(cid){fct_get_cat_ajax(cid,1);}
function fct_search(str){$.history.load("S:"+str); return false;}



function init_panier(){

  // set prod quantities in site same as in panier 
  $('.site_qte, ').each(function(){
    
    var id = $(this).attr("id").substring(4);
    
    if($('#qte2\\['+id+'\\]').length <= 0 || $('#qte2\\['+id+'\\]').val()<=0){
      $('#load_'+id).html('');
      $(this).val(0);
    }else{
      $('#load_'+id).html('<img src="images/icon_checked_2.gif">');
      $(this).val($('#qte2\\['+id+'\\]').val());
    }
  });

  $('.q1').click(function(){
    var aval = parseInt($(this).prevAll('input').val());
    $(this).prevAll('input').val(aval+1);
  });
  
  $('.q2').click(function(){
    var aval = parseInt($(this).prevAll('input').val());
    if(aval>=1)   
      $(this).prevAll('input').val(aval-1);
  });  
  
  $('.f_qte_s input').focus(function(){$(this).select()});
  
  $('.q2,.q1').dblclick(function(){$(this).click();});
    
}


/* end ajax load prods with history */


function fct_load_more(url,navp_browse){

  $("#loadmore").append('<img src="images/icon_loading.gif">');
  $.ajax({
    url: url,
    cache: false,
    success: function(html){
      $("#loadmore").remove();
      $("#mcontent").append(html);
    }
  });
  
}

function fct_cart_submit(){
  $('#cart_loader_overlay').css('display','block');
  $('#cart').load('ajax_box_panier.php',$('#cartform').serialize());
  $('#cart').ajaxComplete(function(evt, request, settings){
    $('#cart_loader_overlay').css('display','none');
  });  
  return false;
}


function fct_cart_toggle(){
  
  var normal_height=100;
  var grow_height=300;

  if($('#cart').css('height')!=grow_height+'px'){
    $('#cart_container').css('height',(grow_height+34)+'px');
    $('#cart').css('height',grow_height+'px');
    $('#cart_loader_overlay').css('height',grow_height+'px');
    $('#cart_loader_overlay').css('line-height',grow_height+'px');
  }else{
    $('#cart_container').css('height',(normal_height+34)+'px');
    $('#cart').css('height',normal_height+'px');
    $('#cart_loader_overlay').css('height',normal_height+'px');
    $('#cart_loader_overlay').css('line-height',normal_height+'px');
  }
}


function add_prod(prod,qte,add) {
  if($('#load_'+prod).length != 0)
    $('#load_'+prod).html('<img src="images/icon_loading.gif" style="position: relative; top: 1px; margin-right: 2px;">');

  
  $('#cart_loader_overlay').show();
  $('#cart').load('ajax_box_panier.php?Pr_ID=' + prod + '&action=addtobag&qte='+qte+'&a='+add);
  
  $('#cart').ajaxComplete(function(evt, request, settings){
    $('#cart_loader_overlay').hide();
  });
  
  if(qte<=0)
    if($('#load_'+prod))
      $('#load_'+prod).html('');
} 


function mod_q(id,add_val){
  var field = '#qte_'+id;
  mod_qte(field,add_val);
  $('#load_'+id).html('');
}



function mod_qte(field,add_val){
  $(field).val(parseInt($(field).val()) + parseInt(add_val));
  
  if($(field).val()<0)
    $(field).val(0);
}

  


function myshop_button(id,rem,fiche){

  if(fiche){
    var img_loader='icon_loading_myshop.gif';
    var img_checked='icon_myshop_fiche_c.gif';
    var img_unchecked='icon_myshop_fiche.gif';
  }else{
    var img_loader='icon_loading_myshop_s.gif';
    var img_checked='icon_my_c.gif';
    var img_unchecked='icon_my.gif';
  }
  $('#my_shop_'+id).html('<img src="images/'+img_loader+'">');
  
  $.get('ajax_myshop.php?Pr_ID=' + id, function(data){
    
    if(data=='0')
      $('#my_shop_'+id).html('<img src="images/'+img_unchecked+'">');
    else if(data=='1')
      $('#my_shop_'+id).html('<img src="images/'+img_checked+'">');
    else
      $('#my_shop_'+id).html('err');
    
    
    if(data=='0' && rem==1)
       $('#box_'+id).remove();
  });
}


function show_alist(prodid){
  if($('#alist_'+prodid).css('display')=='none'){
    $('#alist_'+prodid).show();
    $('#alist_'+prodid).html('<div style="margin: 5px;"><img src="images/icon_loading.gif" style="position: relative; top: 2px;"> Loading</div>');
    $('#alist_'+prodid).load('ajax_box_alist.php?pid=' + prodid);
  }else{
    $('#alist_'+prodid).hide();
  }
}


function help(hid,width,height,modal){

  if(!width)width=500;  
  if(!height)height=400;  

  if($("#help_"+hid).length == 0){
    $("<div id='help_"+hid+"' title='Help'></div>").dialog({ width: width, height: height, modal: modal, dialogClass: '' });
    $("#help_"+hid).html('<div style="margin: 5px; font-size: 12px;"><img src="images/icon_loading.gif" style="position: relative; top: 2px;"> Loading</div>');
    $("#help_"+hid).load("ajax_box_help.php?hid="+hid);
  }else
    $("#help_"+hid).dialog('open');
    
  
  return false;
}


function getcom(comid){
  if($('#com_'+comid).css('display')=='none'){
    $('#com_'+comid).show();
    $('#com_'+comid).html('<div style="margin: 5px; font-size: 12px;"><img src="images/icon_loading.gif" style="position: relative; top: 2px;"> Loading</div>');
    $('#img_com_'+comid+' img').attr('src','images/icon_drop_up.gif');
    $('#com_'+comid).load('ajax_box_com_detail.php?comid=' + comid);
  }else{
    $('#com_'+comid).hide();
    $('#img_com_'+comid+' img').attr('src','images/icon_drop_down.gif');
  }
}

function alist_save(listid,prodid,qte){
    $.get('ajax_box_alist_action.php?listid=' + listid + '&prid=' + prodid + '&qte=' + qte + '&action=save_qte');  
}



/* general functions */

function go(url){
  window.location.href=url;
  return false;
}

function myload(div,url,loader){
  $(loader).show();
  $(div).load(url);
  $(div).ajaxComplete(function(evt, request, settings){
    $(loader).hide();
  }); 
}


function fct_confirm(url,msg){
  if(confirm(msg)){
    window.location.href=url;
    return false;
  }else{
    return false;
  }
}


/* jquery plugin */

jQuery.fn.ajax_submit = function(){
    var params = jQuery(this).serialize();
    jQuery.ajax({
      type: 'POST',
      url: jQuery(this).attr('action'),
      data: params
    });
    return this;
}

