/*
 * By: Farmer Dane
 * jCart Version 0.1
 */

var jCart = {
	fsdefaults : defaults = {
	docroot : "/",
	listprefix : "ccb-item-",
	cartcount_status : "cc-item-number",
	cartlabeladded : "cart-label-added",
	cartloading : "cart-loading",	
	cartlabel : "cart-label",	
	icon_pdf : '_img/cart/pdf-icon.gif',
	icon_remove : '_img/cart/doc_remove.gif',
	scriptdir : '_inc/cart/',
	togglebutton:
	{
		id : 'doc-cart-close',
		on : '_img/cart/rounded_left_down.gif',
		off : '_img/cart/rounded_left_up.gif'
	},
	cartbody_container: 'cc-body-container',
	cartbody: 'cc-body',
	cartdata: {}
	},
	cartopen: false,
	isbusy: false,
	cartdocs: [],
	cartdocs_count: 0,
	o : null,


	init : function (){
	//this.footer-hasdoc = '<div id="doc-cart-status"></div><span id="doc-cart-export"><a href="javascript:void(0)" onclick="exportPDF()">Export to PDF</a></span>';


	},
		embed: function(options){

			this.o = $.extend({}, this.fsdefaults, options);
			this.init();

			if(this.o.cartdata.length > 0){
				alert("parse data");
			}

		},
		addDoc : function(id, pageitem, description){

			if(this.cartdocs[id] === undefined){
				this.cartdocs[id] = [];
				this.cartdocs[id].description = description;

				var rem = "jCart.removeDoc('"+id+"','"+pageitem+"','"+this.escapeSQ(description)+"')"
				var docline = '<div class="ccb-cell clearfix" id="'+this.o.listprefix+id+'"> \
						<div class="ccb-image"><img src="'+this.o.docroot+this.o.icon_pdf+'" alt="PDF Doc" /></div> \
						<div class="ccb-title">'+description+'</div> \
						<div class="ccb-remove"><a href="javascript:void(0)"><img src="'+this.o.docroot+this.o.icon_remove+'" alt="Remove" onclick="'+rem+'" /></a></div> \
					</div><!-- ccb-cell -->';

				$("#"+this.o.cartlabel+"-"+id).hide();
				$("#"+this.o.cartlabeladded+"-"+id).fadeIn(1400);
				$("#"+this.o.cartbody).append(docline);
				this.cartdocs_count++;
				$("#"+this.o.cartcount_status).html(this.cartdocs_count);


				$.post(this.o.docroot+this.o.scriptdir+"mod-doc.php",
				  { thepage: pageitem, id: id, description: description ,  action: "add" },
				  function(data){
				  	//alert(data);
				  }
				);

			}

		},
		removeDoc : function(id,pageitem, description){
			var footer = '<div id="doc-cart-noitems">There are currently no items in your print basket</div>';
			var additem = $('#add-item');
			var addline = '<span class="btn-add-to-basket"><a href="javascript:void(0)" onclick="addDoc(\''+id+'\', \'\', \''+this.escapeSQ(description)+'\')">'+description+'</a></span>';

			if(this.cartdocs[id] !== undefined){
				delete this.cartdocs[id];
				this.cartdocs_count--;
				if(this.cartdocs_count <= 0){
					this.toggleMenu();
					//add blank space for jQuery to force 0 string
					$("#"+this.o.cartcount_status).html(this.cartdocs_count+ "");
				}else{
					$("#"+this.o.cartcount_status).html(this.cartdocs_count);
				}
				$("#"+this.o.listprefix+id).remove();
				$("#"+this.o.cartlabel+"-"+id).fadeIn(1400);
				$("#"+this.o.cartlabeladded+"-"+id).hide();

			}

			$.post(this.o.docroot+this.o.scriptdir+"mod-doc.php",
				  { thepage: pageitem, id: id, action: "remove" },
				  function(data){
				  //alert(data);
				  }
			);

		},
		escapeSQ: function(aString){
			return aString.replace(/\'/, "\\'");
		},
		toggleMenu: function(){
			var togglebutton = $('#'+this.o.togglebutton.id);

			if(this.cartopen){
				togglebutton.attr("src",this.o.docroot+this.o.togglebutton.on);
				this.cartopen = false;
				$("#"+this.o.cartbody_container).slideToggle("slow");				
			}else{
				if(this.cartdocs_count > 0){
					togglebutton.attr("src",this.o.docroot+this.o.togglebutton.off);
					this.cartopen = true;					
					$("#"+this.o.cartbody_container).slideToggle("slow");					
				}
			}
		},
		exportCart: function(){

	jCart.toggleStatusText();
	
	$.post(base+"_flash/downloadpdf.php",
  { cookiename: doccart, lang: 'en', docroot: base , request: 'ajax' },
	  function(data){
	    var data = data.split(':');
	    //alert(data[0] + " - "  + data[1]);
	  	if(data[0].toLowerCase() == 'error'){
	  		alert(data[1]);
	  	}else if(data[0].toLowerCase() == 'fileurl'){
				MM_openBrWindow(data[1],'pdfreport','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=800,height=600');
	  	}
			jCart.toggleStatusText();    
	  }
	);
			
		},
		
	toggleStatusText: function(){
	
		if(this.isbusy){
			$('#'+this.o.cartloading).hide();	
			$('#cart-close').show();	
			this.isbusy = false;
		}else{
			$('#'+this.o.cartloading).show();	
			$('#cart-close').hide();	
			this.isbusy = true;
		}
	
	}


};


