
$(function(){
  //main site doc ready -->
  var ie6 = ($.browser.msie && ($.browser.version == "6.0")) ? true: false;
  var er_1='<p class="alert">Sorry, you will need to login to view our catalogue.<br/>Only the list of <a title="view list" href="?nav=35&list=34&side=0">Featured Highlights</a> can be viewed.<br/><br/>Account facilities are required for access to our full catalogue and services. Please <a title="contact information" href="?nav=2">contact us</a> for details.<button id="cClose" class="button1 blu">Close</button></p>';
  var er_2='<p class="alert">Sorry, Account facilities are required.<br/>Please <a title="contact information" href="?nav=2">contact us</a>.<button id="poClose" class="button1 blu">Close</button></p>';
  $(".lavaLampWI").lavaLamp({ fx: "backout", speed: 700 });
  
  // Highlight on focus '<input>' : uses CSS
  $("input:not('.button, .skip'),textarea, select").bind('focus',function(){$(this).addClass("highlight");}).blur(function(){$(this).removeClass("highlight");});
  $('#results').Zebra('striped');
  $('a#msgClose').live("click",function(){$(this).closest('#myAlerts').hide();});
  
  //user widgets
  $('#wdHolder table.widget-table, #ltImages '). widFilter()

  $('.aClick').click(function(){var u= $(this).attr('href');window.location.href=u;return false;});
  if(ie6){$('#splash img').each(function(){$(this).mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});});ibutton();}
  $(".xClick").popin({loaderImg : 'gfxred/loading2.gif',bodyHTML:'<div class="popin"><div class="cHolder">{0}</div></div>',bodyClass:'popin',contClass:'cHolder',content:'<div id="popOver" style="display:block;"></div><div id="popOverCnt" style="display:block;">'+ er_1 +'</div>',center:"#popOverCnt"});
  $(".tClick").popin({loaderImg : 'gfxred/loading2.gif',bodyHTML:'<div class="popin"><div class="popin-content">{0}</div></div>',bodyClass:'popin',contClass:'popin-content'});
  tooltip();

  $('a.ithumb').toggle(function(){$('img.ithumb').hide('slow'); $('tr.first, tr.second').css("background-image","url()");return false;},function(){$('img.ithumb').show('fast');$('tr.first, tr.second').attr("style","none");return false;});

  // A callback for logging notifications.
  $.jGrowl.defaults.closer = function() {console.log("Closing everything!", this);};
  $.jGrowl.defaults.log = function(e,m,o) {$('#logs').append("<div><strong>#" + $(e).attr('id') + "</strong> <em>" + (new Date()).getTime() + "</em>: " + m + " (" + o.theme + ")</div>")}	;			

  // tickbox
  $('#myForm1 input[type=checkbox]').checkbox({cls:'jquery-tickbox'});
   
  // dumperclose
  $('#dumper .dmphead a').live('click',function(){ $('#dumper').fadeOut('normal');});
  
  //orders & detail doc ready -->
  //common stuff
  $('#contactF').submit(function(){var data=$(this).serialize(); $.post('contacts.asp',data,function(result){$.jGrowl(result,{life:10000});}); return false;});
  $("input#cClose,button#cClose").live('click',function(){e=jQuery.Event("keydown");e.keyCode=27;$(this).trigger(e);return false;});
  $("#fClose").live('click',function(){showOrderForm(0);return false;});
  $("#jumper").change(function(){var u=$(this).val(); if(u!='0') window.location.href=u;return false;});
  
  //detail stuff
  $("button#_vAdd").live('click',function(){postForm("Add","addItem");return false;});
  $("#_vCart").live('click',function(){swap("orders.asp?nav=9");return false;});
  $("#adminedit").live('click',function(){var u = $(this).attr('href'); swap(u);return false;});
  $("#_xAdd").live('click',function(){showOrderForm(1);return false;});
  $("#_xMSG").live('click',function(){var veil=$('#popOver');var cnt=$('#popOverCnt');veil.slideDown(500,function(){cnt.html(er_2).jCenter().fadeIn('fast');});return false;});
  $("#poClose").live('click',function(){ var veil=$('#popOver');var cnt=$('#popOverCnt');cnt.fadeOut('fast',function(){veil.slideUp('fast');});return false;});
  $("#locClik").live('click',function(){var u=$(this).attr('ref');var v=$('#locVal').val();var veil=$('#popOver');var cnt=$('#popOverCnt');var ldr=(u=='locedit')?'orders':'details';var lm=(ie6)?0:150;var tm=(ie6)?0:128; var cssObj={'margin':tm+'px 0 0 '+lm+'px','background':'url("gfxred/button_bg.gif") repeat-x 0 -1px #CCCCCC'};veil.slideDown(500,function(){cnt.load(ldr+'.asp?lid='+u+'&ldata='+v).css(cssObj).fadeIn('fast');});return false;});
  $("#Lupdate").live('click',function(){var veil=$('#popOver');var cnt=$('#popOverCnt');var out="";var outx="";var fields = $(':input','form#locForm').serializeArray();var ct=0;var vout="";var locname;var locval;jQuery.each(fields, function(i, field){if(field.value=="Update" || field.value=="Cancel"){}else{switch(ct){case 0: locname=field.value; break;	case 1: locval=field.value; break;}ct++;} if(ct>=2){if(locname){out=out+locname;outx=outx+locname;if(locval){out=out+"_"+locval+",";outx=outx+"-"+locval+", ";}else{out=out+",";outx=outx+", ";}}ct=0;}});out=out.substring(0, out.length - 1);outx=outx.substring(0, outx.length - 2);$('#locVal').val(out);$('#locClik').text(outx);cnt.fadeOut('fast',function(){cnt.empty();veil.slideUp('fast');});return false;});
  $("#_nItem, #_pItem").live('click',function(){var u=$(this).attr('ref');swap("details.asp?it="+u);return false;});
  
  // order stuff
  $("button#cSend,button#cAuthorise").live('click',function(){var chk=$("input#PON").val();var id=this.id;if(chk!=undefined)chk=chk.replace(/^\s+|\s+$/g, '');if(chk==""||chk==undefined){ var veil=$('#popOver'); var cnt=$('#popOverCnt');var content='<p class="alert">Sorry, you will need to enter a <b>Purchase Order Number</b> to complete<br/>the sending process.<br/>If you do not require this, enter \'<b>na</b>\' in the field.<button id="poClose" class="button1 blu">Close</button></p>';if(id=="cAuthorise") content='<p class="alert">Sorry, you will need to enter a <b>Purchase Order Number</b> to complete<br/>the sending process.<button id="poClose" class="button1 blu">Close</button></p>'; veil.slideDown(500,function(){cnt.html(content).jCenter().fadeIn('fast');});}else{ if(id=="cAuthorise"){$.post('authorise.asp?swt=1', $("form#cCart").serialize(), function(result){chk=(result.indexOf("PDF Saved") > -1)?200:201;$("div.popin-content").html(result);return false;});}else{$.post('printer.asp?swt=1&nav=1', $("form#cCart").serialize(), function(result){chk=(result.indexOf("PDF Saved") > -1)?200:201;$("div.popin-content").load("orders.asp?nav="+chk);return false;});}} return false;});
   $("button#cOrder").live('click',function(){var cc=randomFix();swap("orders.asp?nav=93&ccx="+cc);return false;});
   $("button#cCancel").live('click',function(){var cc=randomFix();swap("orders.asp?nav=91&ccx="+cc);return false;});
   $("button#cCancelNo").live('click',function(){var cc=randomFix();swap("orders.asp?nav=9&ccx="+cc);return false;});
   $("a.cISBN").live('click',function(){var u=$(this).attr('href');swap(u);return false;});
   $("button#cCancelYes").live('click',function(){var cc=randomFix();swap("orders.asp?action=Cancel&ccx="+cc);$('#_myCart').remove();return false;});
   $("button#cDeletMe").live('click',function(){var cc=randomFix();swap("orders.asp?action=DeleteOrder&ccx="+cc);return false;});
   $("button#cEdit").live('click',function(){postForm("Edit","cCart");return false;});
   $("button#cNewOrder").live('click',function(){postForm("NewOrder","cCart");$('#jGrowl').jGrowl('shutdown');return false;});
   $("button#cSave").live('click',function(){postForm("SaveOrder","cCart",function(result){$.jGrowl(result);});return false;});
   //$("button#cSend").live('click',function(){$.post('printer.asp?swt=1&nav=1', $("form#cCart").serialize(), function(result){chk=(result.indexOf("PDF Saved") > -1)?200:201;$("div.popin-content").load("orders.asp?nav="+chk);});return false;});
   $("button#cPrint").live('click',function(){var rf=encodeURI($(this).attr('ref'));ld=(rf)?"?load="+rf:"";$.post('printer.asp'+ld, $("form#cCart").serialize(), function(result){chk=(result.indexOf("PDF Saved") > -1)?100:101;$("div.popin-content").load("orders.asp?nav="+chk+"&kdx="+rf);});return false;});
   $("button#cView").live('click',function(){var rf=encodeURI($(this).attr('ref'));rf=(rf)?rf:"";$("div.popin-content").load("orders.asp?"+rf);return false;});
   $("#tabNav a").live('click',function(){ var u = $(this).attr('href'); var splitRef = u.split('#');u="#" + splitRef[1];var c = $(u);$("#tabNav a").removeClass('active');$(this).addClass('active');$("#itemTabs ._itemTab").not(c).hide('fast',function(){c.fadeIn(555);}); return false;});
   $("button.vEdit").live('click',function(){var r= $(this).attr('ref');$("div#itemForm").load("orders.asp?nav=80&eref="+r,function(){showOrderForm(1);});return false;});
   $("button#cUpdate").live('click',function(){postForm("Update","editItem");return false;});
   $("button#cContinue").live('click',function(){postForm("Continue","cCart");$('#jGrowl').jGrowl('shutdown');return false;});
   $("button#cDelete").live('click',function(){postForm("Delete","editItem");return false;});
   $("button#cRefresh").live('click',function(){var u= $(this).attr('href');if(!u) u="?"; window.location.href=u;return false;});
   $("button#cRestore").live('click',function(){var cc=randomFix();swap("orders.asp?nav=52&ccx="+cc);return false;});
   $("button#cDRestore").live('click',function(){var cc=randomFix();swap("orders.asp?nav=53&ccx="+cc);return false;});
   $("#SPLR").live('change',function(){postForm("UpdateSupplier","cCart",function(result){$.jGrowl(result);});return false;});
  // authorise stuff 
   //$("button#cAuthorise").live('click',function(){$.post('authorise.asp?swt=1', $("form#cCart").serialize(), function(result){chk=(result.indexOf("PDF Saved") > -1)?200:201;$("div.popin-content").html(result);});return false;});
   $('button#aClose').live('click',function(){var u= $(this).attr('ref');window.location.href=u;return false;});
   $("a.aISBN").live('click',function(){var u=$(this).attr('href');$("div#cMask").load(u,function(){$(this).slideDown(555)});return false;});
   $("button#mClose").live('click',function(){$("div#cMask").slideUp('fast',function(){$(this).empty();});return false;});
   $("button#cAPrint").live('click',function(){var u= encodeURI($(this).attr('ref'));var prtr='printer.asp?'+u; var artr='authorise.asp?'+u;$.post(prtr, $("form#cCart").serialize(), function(result){chk=(result.indexOf("PDF Saved") > -1)?100:101;$("div.popin-content").load(artr+"&nav="+chk);});return false;});
   $("button#cAView").live('click',function(){var u= encodeURI($(this).attr('ref'));$("div.popin-content").load("authorise.asp?"+u);return false;});
   $("button#aRemoveZero").live('click',function(){var u= $(this).attr('ref');swap(u);return false;});
   //$("button#aMerge").live('click',function(){$.post('merged.asp?act=merge', $("form#cMerge").serialize(), function(result){$("div.popin-content").html(result);});return false;});
   $("button#aMerge").live('click',function(){var post=$('form#cMerge :input').serializeArray();var chk2="";var chk1="";$.each(post,function(i,fld){if(fld.name=="mrgRefX") chk2=(fld.value==undefined)?"":fld.value.replace(/^\s+|\s+$/g, '');if(fld.name=="mrgRef") chk1=(fld.value==undefined)?"":fld.value.replace(/^\s+|\s+$/g, '');});var chk=(chk1=="")?chk2:chk1;if(chk==""){var veil=$('#popOver');var cnt=$('#popOverCnt');var content='<p class="alert">Sorry, you will need to enter a <b>New Merged Order ref</b> or<br/>select one from the list.<button id="poClose" class="button1 blu">Close</button></p>';veil.slideDown(500,function(){cnt.html(content).jCenter().fadeIn('fast');});}else{ $.post('merged.asp?act=merge', $("form#cMerge").serialize(), function(result){$("div.popin-content").html(result);return false});}return false;});
 
  // merge stuff
   $("a.aQTY").live('click',function(){var u=$(this).attr('href');$("div#cMask").load(u,function(){$(this).slideDown(555)});return false;});
   $("button#cMPrint").live('click',function(){var u=encodeURI($(this).attr('ref'));$.post('printer.asp?'+u, $("form#cCart").serialize(), function(result){chk=(result.indexOf("PDF Saved") > -1)?100:101;$("div.popin-content").load("merged.asp?nav="+chk+"&"+u);});return false;});
   $("button#cMView").live('click',function(){var u= encodeURI($(this).attr('ref'));$("div.popin-content").load("merged.asp?nav=0"+u);return false;});
   $("button#cMAuthorise").live('click',function(){$.post('merged.asp?swt=1', $("form#cCart").serialize(), function(result){chk=(result.indexOf("PDF Saved") > -1)?200:201;$("div.popin-content").html(result);});return false;});
   $("#itemlist a.cMSummary").live('click',function(){var u= $(this).attr('href');$("div#cMask2").load(u,function(){$(this).slideDown(555)});return false;});
   $("button#m2Close").live('click',function(){$("div#cMask2").slideUp('fast',function(){$(this).empty();});return false;});
   $("button#cMUnMerge").live('click',function(){$.post('merged.asp?act=unmerge', $("form#cMerge").serialize(), function(result){$("div.popin-content").html(result);});return false;});
});
