var brand = '';
var type = 1;
var m2 = '';

function init(b,t) {
  brand = b;
  type = t;
  if(t == 1) {
    m2 = getMenu2t1();
    $('#topbox .col1').html(getLoadGif());
    $('#topbox .col2').html(getLoadGif());
    getTopBox1Col1(b);
  }
  if(t == 2) {
    $('#topbox .col1').html(getLoadGif());
    $('#downbox .col1').html(getLoadGif());
    $('#downbox .col2').html(getLoadGif());
    getTopBox2Col1(b);
  }
}

function setAToNormal(v) {
  $(v).each( function() {
    $(this).css("font-weight","normal");
  });
}

function changeToPicBox() {
  $('#htobox').css("position","absolute");
  $('#htobox').css("left","-5000px");
  $('#picbox').css("position","relative");
  $('#picbox').css("left","0px");
}

function changeToHtoBox() {
  $('#picbox').css("position","absolute");
  $('#picbox').css("left","-5000px");
  $('#htobox').css("position","relative");
  $('#htobox').css("left","0px");
}

function getTopBox1Col1(b) {
  changeToHtoBox();
  $.getJSON("cw/prog/cw.php?b="+b+"&c=t1&uid="+user_id,
    function(da){
      var data = da['dat'];
      $('#topbox .col1').html("");
      if( ah > 0 ) { var startid = ah-1; } else { var startid = data[0]['id']; }
      getBox1Col2(b,startid);
      var fw = "";
      $.each(data, function(i,item){
        if( startid == item['id'] ) { fw = "font-weight:bold;"; } else { fw = ""; } 
        $('#topbox .col1').append("<h1><a idnr='"+item['id']+"' style='cursor:pointer;"+fw+"'>"+item['name']+"</a></h1>");
      });
      $('#topbox .col1 a').click(function () { 
        var id = $(this).attr('idnr');
        setAToNormal('#topbox .col1 a');
        $(this).css("font-weight","bold");
        $('#topbox .col2').html(getLoadGif());
        getBox1Col2(b,id);
      });
    });
}

function getTopBox2Col1(b) {
  changeToHtoBox();
  $.getJSON("cw/prog/cw.php?b="+b+"&c=t1&uid="+user_id,
    function(da){
      var data = da['dat'];
      $('#topbox .col1').html("");
      if( ah > 0 ) { var startid = ah-1; } else { var startid = data[0]['id']; }
      getBox2Col1(b,startid);
      getBox2Col2(b,startid);
      var fw = "";
      $.each(data, function(i,item){
        if( startid == item['id'] ) { fw = "font-weight:bold;"; } else { fw = ""; } 
        $('#topbox .col1').append("<h1><a idnr='"+item['id']+"' style='cursor:pointer;"+fw+"'>"+item['name']+"</a></h1>");
      });
      $('#topbox .col1 a').click(function () { 
        var id = $(this).attr('idnr');
        setAToNormal('#topbox .col1 a');
        $(this).css("font-weight","bold");
        $('#downbox .col1').html(getLoadGif());
        $('#downbox .col2').html(getLoadGif());
        getBox2Col1(b,id);
        getBox2Col2(b,id);
      });
    });
}

function getBox1Col2(b,id) {
  changeToHtoBox();
  $.getJSON("cw/prog/cw.php?b="+b+"&c=t3&id="+id+"&uid="+user_id,
    function(da){
      var data = da['dat'];
      var img = '';
      $('#topbox .col2').html("");
      $.each(data, function(i,item){
        img = '';
        if( item['src'] != null && item['src'] != '' ) { img = "<img src='/cw/images/"+b+"/"+item['src']+"' title='"+item['src']+"' /><br>"; }
        if( da['uid'] > 0 ) {
          $('#topbox .col2').append("<div class='jsb2c2'>"+img+item['name']+"<br><a idnr='"+item['id']+"' style='cursor:pointer;'>Pick this product</a></div><div class='hrdashed'></div>");
        } else {
          $('#topbox .col2').append("<div class='jsb2c2'>"+img+item['name']+"</div><div class='hrdashed'></div>");
        }
      });
      if( da['uid'] > 0 ) {
        $('#topbox .col2 a,#topbox .col2 img').click(function () { 
          var bid = $(this).parent().children("a").attr('idnr');
          setAToNormal('#topbox .col2 a');
          $(this).parent().children("a").css("font-weight","bold");
          changeToPicBox();
          getPriceBox(b,id,bid);
        });
      }
      if( bh > 0 && da['uid'] > 0 ) {
        setAToNormal('#topbox .col2 a');
        $(this).parent().children("a").css("font-weight","bold");
        changeToPicBox();
        getPriceBox(b,id,bh-1);
      }
    });
}

function getBox2Col1(b,id) {
  changeToHtoBox();
  $.getJSON("cw/prog/cw.php?b="+b+"&c=t2&id="+id+"&uid="+user_id,
    function(da){
      var data = da['dat'];
      var img = '';
      if( data.length > 0 ) { $('#downbox .col1').html("<h1>Pick by Pattern</h1>"); } else { $('#downbox .col1').html(""); }
      $.each(data, function(i,item){
        img = '';
        if( item['src'] != null && item['src'] != '' ) { img = "<img src='/cw/images/"+b+"/"+item['src']+"' title='"+item['src']+"' /><br>"; }
        if( da['uid'] > 0 ) {
          $('#downbox .col1').append("<div class='jsb2c1'>"+img+item['name']+" <a idname='"+item['name']+"' style='cursor:pointer;'>Pick this pattern</a></div>");
        } else {
          $('#downbox .col1').append("<div class='jsb2c1'>"+img+item['name']+"</div>");
        }
      });
      if( da['uid'] > 0 ) {
        $('#downbox .col1 a,#downbox .col1 img').click(function () { 
          var bname = $(this).parent().children("a").attr('idname');
          setAToNormal('#downbox .col1 a');
          setAToNormal('#downbox .col2 a');
          $(this).parent().children("a").css("font-weight","bold");
          changeToPicBox();
          getPricePattern(b,id,bname);
        });
      }
      if( ph != "" && da['uid'] > 0 ) {
        setAToNormal('#downbox .col1 a');
        setAToNormal('#downbox .col2 a');
        $(this).parent().children("a").css("font-weight","bold");
        changeToPicBox();
        getPricePattern(b,id,ph);
      }
    });
}

function getBox2Col2(b,id) {
  changeToHtoBox();
  $.getJSON("cw/prog/cw.php?b="+b+"&c=t3&id="+id+"&uid="+user_id,
    function(da){
      var data = da['dat'];
      var img = '';
      $('#downbox .col2').html("<h1>Pick by Product</h1>");
      $.each(data, function(i,item){
        img = '';
        if( item['src'] != null && item['src'] != '' ) { img = "<img src='/cw/images/"+b+"/"+item['src']+"' title='"+item['src']+"' /><br>"; }
        if( da['uid'] > 0 ) {
          $('#downbox .col2').append("<div class='jsb2c2'>"+img+item['name']+"<br><a idnr='"+item['id']+"' style='cursor:pointer;'>Pick this product</a></div><div class='hrdashed'></div>");
        } else {
          $('#downbox .col2').append("<div class='jsb2c2'>"+img+item['name']+"</div><div class='hrdashed'></div>");
        }
      });
      if( da['uid'] > 0 ) {
        $('#downbox .col2 a,#downbox .col2 img').click(function () { 
          var bid = $(this).parent().children("a").attr('idnr');
          setAToNormal('#downbox .col2 a');
          setAToNormal('#downbox .col1 a');
          $(this).parent().children("a").css("font-weight","bold");
          changeToPicBox();
          getPriceBox(b,id,bid);
        });
      }
      if( bh > 0 && da['uid'] > 0 ) {
        setAToNormal('#downbox .col2 a');
        setAToNormal('#downbox .col1 a');
        $(this).parent().children("a").css("font-weight","bold");
        changeToPicBox();
        getPriceBox(b,id,bh-1);
      }
    });
}

function drawPriceBox(da) {
  var data = da['dat'];
  var lclass = "";
  var lflag = 0;
  var pnumlist = "";
  var spacer = "";
  $('#boplbox .txtpiclist').html("");
  $.each(data, function(i,item) {
    if( item['ty'] == 'b' ) {
      $('#boplbox .txtpiclist').append("<div class='ph1'>"+item['name']+"</div>");
      lflag = 0;
    } else {
      if( lflag == 0 ) { lclass = "odd"; lflag = 1; } else { lclass = "even"; lflag = 0;}
      $('#boplbox .txtpiclist').append("<div class='pline'><div class='inline "+lclass+"'><div class='tab1'><b>"+item['pnum']+"</b> "+item['pattern']+"</div><div class='tab2'>"+item['price']+"</div><input class='tab3' id='"+item['pnum'].replace(/\//,"_")+"' pid='"+item['pnum']+"' type='text' name='' value=''></div></div>");
      pnumlist += spacer+item['pnum'];
      spacer = ":";
    }
  });
  $('#boplbox input').css(
    {'background-image':'url(images/ajax-loader.gif)',
     'background-repeat':'no-repeat' 
    });
  $.getJSON("cw/prog/basket.php?c=getlist&uid="+user_id+"&pl="+pnumlist,insertCountList);
  $.getJSON("cw/prog/basket.php?c=getlocal&uid="+user_id+"&pl="+pnumlist,updateLocalPriceField);
}

function insertCountList(da) {
  var data = da['dat'];
  $.each(data, function(i,item) {
    $('#'+item['pnum'].replace(/\//,"_")).css('background-image','');
    if( item['pc'] == 0 || item['pc'] == null ) {
      $('#'+item['pnum'].replace(/\//,"_")).attr('value','');
    } else {
      $('#'+item['pnum'].replace(/\//,"_")).attr('value',item['pc']);
    }
  });
  $('#boplbox input').change(changeCountInput);
}

function changeCountInput() {
  $.getJSON("cw/prog/basket.php?c=add&uid="+user_id+"&count="+$(this).attr('value')+"&pid="+$(this).attr('pid'), function(da) {
    var pnumlist = "";
    var spacer = "";
    $('#boplbox input').each( function() {
      pnumlist += spacer+$(this).attr('pid');
      spacer = ":";
    });
    $.getJSON("cw/prog/basket.php?c=getlocal&uid="+user_id+"&pl="+pnumlist,updateLocalPriceField);
  });
}

function updateLocalPriceField(da) {
  var data = da['dat'];
  $('#charea').html("<div id='cha1' style='position:relative;width:410px;'></div>");
  $('#charea #cha1').html("<img src='images/oroben.gif'>");
  $('#charea #cha1').append("<div id='chab' style='position:relative;border:0px;width:410px;background-color:#f7eee7;'></div>");
  $('#charea #chab').append("<div style='position:relative;float:left;margin:0px 25px 0px 20px;'>Total Items <b>"+data['global']+"</b><br>Total Value <b>"+data['globalprice']+"</b></div>");
  $('#charea #chab').append("<div style='position:relative;float:left;margin:0px 25px 0px 0px;'>Items in search <b>"+data['local']+"</b><br>Local Value <b>"+data['localprice']+"</b></div>");
  $('#charea #chab').append("<div style='position:relative;float:right;margin:0px 15px 0px 0px;'><img src='images/add_to_order_ws.gif' border='0' style='cursor:pointer;margin:0px 0px 0px 0px'><br><a href='/?content=shopping_trolley'><img src='images/checkout.gif' border='0' style='cursor:pointer;margin:5px 0px 0px 0px'></a></div>");
  $('#charea #chab').append("<div class='clboth'></div>");
  $('#charea #cha1').append("<img src='images/orunten.gif'>");
}

function getPriceBox(b,aid,bid) {
  $('#boplbox .txtpiclist').html(getLoadGif());
  $.getJSON("cw/prog/cw.php?b="+b+"&c=t4&aid="+aid+"&bid="+bid+"&uid="+user_id,drawPriceBox);
}

function getPricePattern(b,aid,bname) {
  $('#boplbox .txtpiclist').html(getLoadGif());
  $.getJSON("cw/prog/cw.php?b="+b+"&c=t5&aid="+aid+"&bname="+bname+"&uid="+user_id, drawPriceBox);
}


function getLoadGif() {
  return "<img src='images/ajax-loader.gif'>";
}

function getMenu2t1() {
  return $('#topbox .col2').html();
}




