$(document).ready(function(){

// lightbox init
//$('a[rel=lightbox]').lightBox();

var orders = $('div#orders');
if (orders.html() != '') { orders.addClass('box'); }

//if ($('span.shopping-cart').hasClass('throw-away')) { $(this).hide(); }

// scroll
$('.scrollable').scrollable({
				items: ".items",
				item: "li",
				next: "_next",
				prev: "_prev",
				size: 3
			});

// form
$('#submit_button').live('click', function(){
    $('#loading').fadeIn();
    $('#ajax_form').ajaxSubmit({ 
    type: 'post', 
    success: showResponse
   }); 
});

function showResponse(responseText)
{
  $('#loading').fadeOut();
  var arr = responseText.split('&');
  var target = $('#'+arr[2]);
  if (arr[0] == 'success') { document.location.href='/'; }
  else
  {
   target.fadeTo('slow', 0.30);
   target.html(arr[1]);
   target.fadeTo('slow', 1.0);
   return false;
  }
}

$('.amount').live('click', function(){
  $(this).val('');  
}); 

// calculate
$('.amount').live('keyup', function(){
var arr = new Array();
for(var j=0; j<100; j++) {arr[j]=j;}
 var i = arr;
 var total = 0;
 if ($(this).val() in i)
 {
   var id = $(this).attr('id');
   var mult = $(this).val();
   var price = $('.amount ~ span#pricein_'+id).html(); 
   $('.amount ~ span#priceout_'+id).html(price*mult);
   $('.amount ~ :input.amount_id_'+id).val(mult);
   $('span#priceout_'+id+' ~ :input.price_chk').val(price*mult);
      
  }
 $('span.price').each(function(){
   var am = $(this).html();
   total = parseInt(total) + parseInt(am);
  }); 
  
 $('span#total_sum').html(total); 
  
  //$(this).trigger('focus');
}); 


$('span.shopping-cart').live('click', function(){
  $(this).fadeTo('slow', 0.30);
  if ($(this).hasClass('exclude'))
  {
   $('#calc').fadeTo('fast', 0.50);
   doSend($(this).attr('id'), '', $('#calc'), null);
  }
  else
  {
   orders.fadeTo('fast', 0.50).addClass('box');
   doSend($(this).attr('id'), '', orders, null);  
  }
  if ($(this).hasClass('throw-away')) 
  { $(this).removeClass('throw-away'); $(this).attr('id', str_replace('remove', 'add', $(this).attr('id'), 1)); } 
  else 
  { $(this).addClass('throw-away'); $(this).attr('id', str_replace('add', 'remove', $(this).attr('id'), 1)); }
});

});

function doSend(url, data, target, progress_bar)
{
  $.ajax({
	   type: 'POST',
	   cache: true,
	   url: url,
	   dataType: 'html',
	   data: data,
	   ajaxSend : function() { },
	   success: function(response){
	   target.html(''); target.html(response); target.fadeTo('fast', 1.0);  
	   if (progress_bar != null) {
		 //progress_bar.fadeOut('fast'); $('.content_div').fadeTo('fast', 1.0);
		 }
	  }
   });
}

function str_replace(search, replace, subject, count)
{
var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

