// JavaScript Document
//
(function($) {
//
var error = true;

// plugin definition
$.fn.basket = function(options) {
	$this = $(this);
	$.fn.basket.defaults  = $.extend({}, $.fn.basket.defaults, options);
	$this.bind("click",function(){$.fn.basket.showBasket()});
	$(".add").bind("click",function(){$.fn.basket.add()});
	//$.fn.basket.refresh_small();
	$.fn.basket.init();
	/*if($.fn.basket.defaults.notifier)$this
	$this.bind('keyup',function(){
	})*/
};
//
$.fn.basket.defaults = {
  minchars : 1,
  notifier : true,
  url : "http://" + document.domain + "/ajax_handlers/basket_handler.php" 
  
}
//
//
$.fn.basket.showBasket = function(){
  /*$.get($.fn.basket.defaults.url,{job:'show'},
		function(data){			
		}
  )*/
   $("#wrapper").css({'opacity':'0.5'});
   $("#loader").remove();
   //$("body").append('<img src="/images/ajax-loader-bar.gif" id="loader"/>');
   //center("#loader");
   $.ajax({
    type: "GET",
    url: $.fn.basket.defaults.url,
    data: {job:'show'},
    contentType: "text/html; charset=utf-8",
    success: function(data) {
      $(".basket").remove();
			$("body").append(data);
			//setTimeout(function(){center(".basket")},1000);
			center(".basket")
			$.fn.basket.init();
			$("#wrapper").css({'opacity':'1'});
			//$("#loader").remove();
			//alert(data);
    }
  });

}
$.fn.basket.init = function(){
  	/*if(window.console){
	  if(console){
		console.log("OK")  
	  }
	}*/
	$(".basket").draggable();
	$("#close").bind("click",function(){$(".basket").remove();})
	$("#remove").bind("click",function(){$.fn.basket.remove()});
	$("#clear").bind("click",function(){$.fn.basket.clear()});
	$("#offer").bind("click",function(){$.fn.basket.checkCode()});
	$("#clearCode").bind("click",function(){$.fn.basket.clearCode()});
	$(".view_basket").bind("click",function(){$.fn.basket.showBasket()});
	$(".quantitySelect").each(function(){
	  $(this).bind('change',function(){
	    $.fn.basket.changeQuantity($(this).attr('id'),$(this).val());
	  })
	});
}
$.fn.basket.add = function(id){
  $("#wrapper").css({'opacity':'0.5'});
  //$("body").append('<img src="/images/ajax-loader-bar.gif" id="loader"/>');
  //center("#loader");
  if(id == undefined)var id = $("#id").val();
  var option = ''
  /*$(".options").each(function(){
	option +=$(this).val()+",";						  
  });*/
  if($(".options").val()!= null || $(".options").val() != undefined)option = $(".options").val();
  $.get($.fn.basket.defaults.url,{job:'add',id:id,option : option},function(data){
	$.fn.basket.refresh_small(data)
	$(".basket").remove();
	$("body").append(data);
	//setTimeout(function(){center(".basket")},1000);
	center(".basket")
	$.fn.basket.init();
	$("#wrapper").css({'opacity':'1'});
	//$("#loader").remove();
  })
}
$.fn.basket.remove = function(id){
	//var id = '';
	/*$(".prod").each(function(){
	  $this = $(this);
	  if($this.attr("checked"))id += $this.val() + ':';
	});*/
	$.get($.fn.basket.defaults.url,{job:'remove',id:id},function(data){
	//alert(data)
	$.fn.basket.refresh_small(data)
	$.fn.basket.showBasket();
  })
}
$.fn.basket.clear = function(){
	$.get($.fn.basket.defaults.url,{job:'clear'},function(data){
	$.fn.basket.refresh_small(data)
	$.fn.basket.showBasket();
  })
}
$.fn.basket.re_order = function(id){
	$.get($.fn.basket.defaults.url,{job:'reorder',id:id},function(data){
	$.fn.basket.refresh_small(data)
	$.fn.basket.showBasket();
  })
}

$.fn.basket.refresh_small = function(data,id){
  $.get($.fn.basket.defaults.url,{job:'refresh'},function(data){
	//alert(data)
	var res = data.split("|");
	$('#items').html(res[0]);
	//$('a#checkout,a#checkout2').attr("href",res[2]);
	$('#discount_value').html('&pound;'+res[3])
	$('#hash').val(res[4])
	try{
		console.log(res[4])
		console.log($('#hash'))
	}catch(e){}
	//alert($res[3]);
	
	$('.total').each(function(){$(this).html(res[1])});
  })
}
$.fn.basket.changeQuantity = function(id,q){
  $.get($.fn.basket.defaults.url,{job:'change',id:id,q:q},function(data){
	$.fn.basket.refresh_small(data,id);
	var el =$("#"+id).parent();
	var t = el.next(".price").html();
	t = t.substr(1);
	var p = t*q;
	$("#update").remove();
	$(".basket #total span").prepend('<span style="color:#ff0000;font-weight:normal" id="update">Basket updated </span>')
	
	//for(var i =1;i<3;i++)$(".basket #total span").fadeOut().fadeIn();
	//$("#loader").remove();
	
  })  
}
$.fn.basket.checkCode = function(){
  	var code = $('#code').val();
	$("#offer").replaceWith('<img src="/images/ajax-loader2.gif" style="margin-left:10px;"/>');
	$.get($.fn.basket.defaults.url,{job:'checkCode',code:code},function(data){      
	  $.fn.basket.refresh_small();
	  $(".basket").remove();
	  $("body").append(data);
	  center(".basket")
	  $.fn.basket.init();
	  
	})
}
$.fn.basket.clearCode = function(){
	$("#clearCode").replaceWith('<img src="/images/ajax-loader2.gif"');
	$.get($.fn.basket.defaults.url,{job:'clearCode'},function(data){													   
      $.fn.basket.refresh_small(data);
	  $(".basket").remove();
	  $("body").append(data);
	  center(".basket")
	  $.fn.basket.init();
	})
}
//
//
})(jQuery);

jQuery.extend({
  center: function(el) { return center(el); }
});


function center(el)
{
	//$el = el;
	$el = jQuery(el);
	$el.css('position','absolute');
	var pos = {
       sTop : function() {
         return window.pageYOffset || jQuery.boxModel &&
document.documentElement.scrollTop ||   document.body.scrollTop;
       },
       wHeight : function() {
         if ( jQuery.browser.opera || (jQuery.browser.safari && parseInt
(jQuery.browser.version) > 520) ) {

return window.innerHeight - ((jQuery (document).height() >

window.innerHeight) ? getScrollbarWidth() : 0);
                } else if ( jQuery.browser.safari ) {
                        return window.innerHeight;
                }       else {
           return jQuery.boxModel && document.documentElement.clientHeight
|| document.body.clientHeight;
         }
       }
     };
	$el.css('top', pos.sTop() + (pos.wHeight() / 2) - (/*$el.height()*/200 / 2));  
	$el.css('left', jQuery(window).width()/2-$el.width()/2);  
}
/**********/
function getScrollbarWidth(){

 var scrollbarWidth = 0;

if ( !scrollbarWidth ) {
if ( $.browser.msie ) {
var $textarea1 = $('<textarea cols="10" rows="2"></textarea>')
.css({ position: 'absolute', top: -1000, left: -1000 }).appendTo('body'),
$textarea2 = $('<textarea cols="10" rows="2" style="overflow: hidden;"></textarea>')
.css({ position: 'absolute', top: -1000, left: -1000 }).appendTo('body');
scrollbarWidth = $textarea1.width() - $textarea2.width();
$textarea1.add($textarea2).remove();
} else {
var $div = $('<div />')
.css({ width: 100, height: 100, overflow: 'auto', position: 'absolute', top: -1000, left: -1000 })
.prependTo('body').append('<div />').find('div')
.css({ width: '100%', height: 200 });
scrollbarWidth = 100 - $div.width();
$div.parent().remove();
}
}
return scrollbarWidth;

}

