var clip=null;
var clip2=null;
var linktag=null;
var linktag2=null;
var gotolink=null;
var gotolink2=null;
var pos;
var x;
var y;
var mouseY=null;
$(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" ){
			
			//infos_arr = infos_li.split(",");
			
			//var rate = Math.ceil(parseInt(infos_arr[0])  /  ( parseInt(infos_arr[0]) + parseInt(infos_arr[1]) ) * 100);
			//$(this).html( 'Success: ' + rate + '% <a><img src="/image/supported.gif" border=0></a> <a><img src="/image/againsted.gif" border=0></a>');
			$(this).empty();
			$(this).parent().find(".li_rolls_block").empty();
		}else{
			//if( typeof(id_arr) == 'undefined' ){
			//	id_arr	=	new Array();
			//}
			//id_arr.push(id);
		}
	});

   $(".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.com'});
				   
				  //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(" Dank für die Stimmabggabe");
				  });
				  
				  $(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");

			   }
		  });

		}
   ); 
   

	ZeroClipboard.setMoviePath('/javascript/ZeroClipboard.swf');
	clip=new ZeroClipboard.Client();
	clip.setHandCursor(true);

	$().mousemove(function(e){x=e.pageX;y=e.pageY;if(y>=(mouseY+20)||y<=(mouseY-20)){nd()};})

	$('.ec a.cc-code').mouseover(function(){
		gotolink=$(this).attr("href");
//		//add by jimmy 20100114
//		ca = $(this).attr("ca");
//		if(ca)
//		{
//			var temp = new Array();
//			temp = ca.split("|");
//			cid = temp[0];
//			pos = temp[1];
//			gotolink = $("#res-" + cid).val() + "&ca=" + pos;
//		}
//		else
//			gotolink=$(this).attr("href");
//		//end
		mouseY=y;
		clip.setText(this.innerHTML);
		linktag=$(this).Attributes;
		if(clip.div){
			clip.receiveEvent('mouseout',null);
			clip.reposition(this);
		}else 
			clip.glue(this);
		clip.receiveEvent('mouseover',null);
	});

	clip.addEventListener('onMouseOut',my_mouse_out_handler);
	clip.addEventListener('onMouseUp',my_mouse_up_handler);
	clip.addEventListener('onMouseOver',my_mouse_over_handler);

	clip2=new ZeroClipboard.Client();
	clip2.setHandCursor(true);
	$('.ec a.img_code').mouseover(function(){
		gotolink2=$(this).attr("href");
//		//add by jimmy 20100114
//		ca = $(this).attr("ca");
//		if(ca)
//		{
//			var temp = new Array();
//			temp = ca.split("|");
//			cid = temp[0];
//			pos = temp[1];
//			gotolink2 = $("#res-" + cid).val() + "&ca=" + pos;
//		}
//		else
//			gotolink2=$(this).attr("href");
//		//end

		//mouseY=y;
		clip2.setText($(this).attr("code"));
		linktag2=$(this).Attributes;
		if(clip2.div){
			clip2.receiveEvent('mouseout',null);
			clip2.reposition(this);
		}else 
			clip2.glue(this);
		clip2.receiveEvent('mouseover',null);
	});

	clip2.addEventListener('onMouseOut',my_mouse_out_handler2);
	clip2.addEventListener('onMouseUp',my_mouse_up_handler2);
		
	if( $("#keyword").val() == "" ){
		$("#keyword").removeClass();
		$("#keyword").addClass("searchbox");
		$("#keyword").val("Mit Shopname oder Domäne erhalten Sie die Gutscheine & Angebote");
	}
	

	$("#keyword").focus( function () { 
		if($(this).val() == "Mit Shopname oder Domäne erhalten Sie die Gutscheine & Angebote"){
			$(this).removeClass();
			$(this).addClass("searchbox2");
			$(this).val("");
		}
	}); 
	
	$("#keyword").blur( function () { 
		if($(this).val() == ""){
			$(this).removeClass();
			$(this).addClass("searchbox");
			$(this).val("Mit Shopname oder Domäne erhalten Sie die Gutscheine & Angebote");
		}
	});	

//	////////////coupon rd///////////////////////
//	//popup + rd in current window
//	var ca = "";
//	var cid = "";
//	var curl = "";
//	var pos = "";
//	$(".rdstyle1").click(function () {
//		ca = $(this).attr("ca");
//		var temp = new Array();
//		temp = ca.split("|");
//		cid = temp[0];
//		pos = temp[1];
//		curl = $("#res-" + cid).val() + "&ca=" + pos;
//		location.href = curl;
//	});
//	
//	//pop up only
//	$(".couponitem_without_code,.rdstyle2").click(function () {
//		ca = $(this).attr("ca");
//		var temp = new Array();
//		temp = ca.split("|");
//		cid = temp[0];
//		pos = temp[1];
//		curl = $("#res-" + cid).val() + "&ca=" + pos;
//		window.open(curl);
//	});
//	////////////////////////////////////////////
})
function my_mouse_over_handler(client){

	return overlib('Klicken Sie zum Kopieren des Codes und öffnen Sie die Seite. Fügen Sie den Code bei der Bezahlung, um den Rabatt zu erhalten.');
}

function my_mouse_out_handler(client){
	//$("#codetips").fadeOut(200);
	clip.hide();
	return nd();
	
}
function my_mouse_out_handler2(client){
	//$("#codeTip").fadeOut(200);
	clip2.hide();
}

function my_mouse_up_handler(client){
	var storeWin=window.open(gotolink);
	if(storeWin){
		return false;
	}else{document.location=gotolink;
	}
}

function my_mouse_up_handler2(client){
	var storeWin=window.open(gotolink2);
	if(storeWin){
		return false;
	}else{document.location=gotolink2;
	}
}


function listMerCatTag(){
	
	var CouponTypeID = parseInt($("#coupontype").val());
	if( CouponTypeID >=1 &&  CouponTypeID <=6 ){
		$("#SMerchants").html("Loading...");
		$("#widgetsubmit").attr("disabled","disabled");
		var arr  = {'act':'list','coupontypeid':CouponTypeID};
		
		$.ajax({
			type: "POST",
			url: "/front/couponwidget.php",
			data: $.param(arr),
			success: function(msg){
				json = eval("("+msg+")");
				if( json.status == true){
					var str = $("<select name='fixid' id='fixid' style='width:250px; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'></select>");
					var option = "";
					var title = "";
					$.each(json.data,function(i,t){
						option += "<option value=\"" + t.id + "\">" + t.name + "</option><br>";			  
					});	
				}
				if( CouponTypeID == 1 || CouponTypeID ==2 ){
					title = "<b>Kategorie wählen:</b><br/>";	
				}else if( CouponTypeID == 3 || CouponTypeID ==4 ){
					title = "<b>Händler wählen:</b><br/>";	
			    }else if( CouponTypeID == 5 || CouponTypeID ==6 ){
					title = "<b>Etikett wählen:</b><br/>";		
				}

				$("#SMerchants").html(title + $(str).html(option).parent().html());
				$("#widgetsubmit").attr("disabled","");
			}
		});
	}else{
		$("#SMerchants").html(" ");	
	}
}

function submitcheck(){
    var obj = document.getElementById('form1');
    if( !Validator.Validate(obj,2) ){
        return false;
    }

    $("#widgetsubmit").attr("disabled","disabled");
    $("#result").html('Generieren Gutschein-Widget...');

    var coupontypeid = parseInt($("#coupontype").val());
    var fixid = parseInt($("#fixid").val());
    var widgetsizeid = parseInt($('input[name=widgetsize]:checked').val());
    var couponnum = parseInt($("#couponnum").val());

    var arr  = {'act':'submit','coupontypeid':coupontypeid,'fixid':fixid,'widgetsizeid':widgetsizeid,'couponnum':couponnum};

    $.ajax({
			type: "POST",
			url: "/front/couponwidget.php",
			cache: true,
			data: $.param(arr),
			success: function(msg){
				$("#widgetsubmit").attr("disabled","");
				//json = eval("("+msg+")");
				//var c = "";
				/*if(json.status == true && json.data != null)
				{
					c = json.data;
					c=c.replace(/&nbsp;/g,' ');
					c=c.replace(/&amp;/g,'&');
					c=c.replace(/&#039;/g,'\'');
					c=c.replace(/&quot;/g,'"');
					c=c.replace(/&lt;/g,'<');
					c=c.replace(/&gt;/g,'>');
                }else{
                    alert('Ihre gewählten Gutscheine sind nicht verfügbar, versuchen Sie einen anderen!');
                }
				*/
				if(msg == "")
				{
					alert('Ihre gewählten Gutscheine sind nicht verfügbar, versuchen Sie einen anderen!');
				}
				else
				{
					$("#result").html(msg);
				}
				//$("#result").html(c);
			}
		});

    return false;
}

function SubscribeEmail(id){
	var val = $("#"+id).val();
	var email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if(  email.test(val) ){
		var arr  = {'email':val};
		$.ajax({
			type: "POST",
			url: "/front/couponalert.php?action=setsinglealert",
			data: $.param(arr),
			success: function(msg){
				json = eval("("+msg+")");
				alert(json.msg);
			}
		});
	}else{
		alert('Bitte verwenden Sie eine gültige E-Mail-Adresse');
	}
}
