var clip = null;
var clip_gotolink = "";
var clip_text = "";

$(document).ready(function(){
           
   $(".li_rolls").each(function(){
		var id	  = $(this).attr("infos");
		var infos_li = $.cookie('rolls_'+ id );
		if( infos_li != null && typeof(infos_li) != "undefined" ){
			$(this).empty();
			$(this).parent().find(".li_rolls_block").empty();
		}else{
		}
	});

   $(".rolls").click(  
		function () {

		   var dollArrs = $(this).attr("infos").split(",");
		   var dollArr  = {'style':dollArrs[0],'id':dollArrs[1],'value':dollArrs[2],'act':1};
		   
		   var obj		=	this;

		  $.ajax({
			   type: "POST",
			   url: "/front/polls.php",
			   data: $.param(dollArr),
			   success: function(msg){
				   json	=	eval("("+msg+")");
				   
				   if( isNaN(json.count[0]) ){
						json.count[0] = 0;   
				   }
				   
				   if( isNaN(json.count[1]) ){
						json.count[1] = 0;   
				   }

				   $.cookie('rolls_'+json.id,json.count[0] +','+json.count[1],{ expires: 7, path: '/', domain: 'couponsnapshot.co.nz'});
				   
				  //var rate = Math.ceil(parseInt(json.count[0])  /  ( parseInt(json.count[0]) + parseInt(json.count[1]) ) * 100);
				  
				  var objp = $(obj).parent();
				  
				  $(obj).parent().fadeOut("slow",function(){
						$(this).css('background-color','EFFEFF');
						$(obj).parent().html(" Thank you for voting ");
				  });
				  
				  $(objp).fadeIn("slow");
				  
				  $(objp).fadeOut("slow",function(){
					  $(objp).css('background-color','');
					  //$(objp).html( 'Success: ' + rate + '% <a><img src="/image/supported.gif" border=0></a> <a ><img src="/image/againsted.gif" border=0></a>');	
					  $(objp).empty();
					  $(objp).parent().find(".li_rolls_block").empty();

				  });
				   
				  //$(objp).fadeIn("slow");

			   }
		  });

		}
   ); 
   
	if( $("#keyword").val() == "" ){
		$("#keyword").removeClass();
		$("#keyword").addClass("searchbox");
		$("#keyword").val("Enter a store name or domain to find coupons & deals");
	}
	

	$("#keyword").focus( function () { 
		if($(this).val() == "Enter a store name or domain to find coupons & deals"){
			$(this).removeClass();
			$(this).addClass("searchbox2");
			$(this).val("");
		}
	}); 
	
	$("#keyword").blur( function () { 
		if($(this).val() == ""){
			$(this).removeClass();
			$(this).addClass("searchbox");
			$(this).val("Enter a store name or domain to find coupons & deals");
		}
	});	

	//tip for receive
	$('.ec .couponitem_without_code').tooltip(get_tooltip_para('receive'));

	$('.ec a.cc-code,.ec a.img_code').each(function(){
		$(this).attr("target","_blank");
	});

	if(window.clipboardData)
	{
		$('.ec a.cc-code,.ec a.img_code').click(function(){
			var code = $(this).attr("code");
			if(!code) code = $(this).text();
			window.clipboardData.setData("Text",code);
		});
		
		$('.ec a.cc-code,.ec a.img_code').tooltip(get_tooltip_para('cc-code'));
	}
	else
	{
		//ZeroClipboard
		ZeroClipboard.setMoviePath('/javascript/ZeroClipboard.swf');
		clip = new ZeroClipboard.Client();
		clip.setHandCursor(true);
		
		//to speedup
		$('.ec a.cc-code,.ec a.img_code').each(function()
		{
			if(clip_text != "") return false;
			process_clip_things(this);
			clip.hide();
		});
		
		//clip.addEventListener('onMouseOver', function(client){
		//	clip.setText(clip_text);
		//});

		clip.addEventListener('onMouseUp', function(client){
			clip.setText(clip_text);
			var storeWin = window.open(clip_gotolink);
			if(storeWin) return false;
			else document.location = clip_gotolink;
		});
		
		clip.addEventListener('onMouseOut',function(client){
			clip.hide();
		});
		
		$('.ec a.cc-code,.ec a.img_code').mouseover(function()
		{
			process_clip_things(this);
		});
	}
})

function process_clip_things(obj)
{
	jq_obj = $(obj);
	clip_gotolink = jq_obj.attr("href");
	var code = jq_obj.attr("code");
	if(!code) code = jq_obj.text();
	clip_text = code;
		
	if(clip.div) clip.reposition(obj);
	else 
	{
		clip.glue(obj);
		$(clip.div).tooltip(get_tooltip_para('cc-code'));
	}
}

function get_tooltip_para(tp)
{
	var p = {};
	switch (tp)
	{
		case "receive":
			p = {
					delay: 0,
					showURL: false,
					bodyHandler: function(){ return 'Click to receive the discount and open site.';}
				};
			break;
		case "cc-code":
			p = {
					delay: 0,
					showURL: false,
					bodyHandler: function(){ return 'Click to copy this code and open site.Paste the code during check out to receive the discount.';}
				};
			break;
	};
	return p;
}
