//resize the images
function resizeimg(ImgD,iwidth,iheight) { 
     var image=new Image(); 
     image.src=ImgD.src; 
     if(image.width>0 && image.height>0){ 
        if(image.width/image.height>= iwidth/iheight){ 
           if(image.width>iwidth){ 
               ImgD.width=iwidth; 
               ImgD.height=(image.height*iwidth)/image.width; 
           }else{ 
                  ImgD.width=image.width; 
                  ImgD.height=image.height; 
                } 
        } 
        else{ 
                if(image.height>iheight){ 
                       ImgD.height=iheight; 
                       ImgD.width=(image.width*iheight)/image.height; 
                }else{ 
                        ImgD.width=image.width; 
                        ImgD.height=image.height; 
                     } 
            } 
    } 
}

//open store
function openStore(url) {
var load = window.open(url,'_blank','');
}

//tips show

function copy(text)  
{  
      if(window.clipboardData)  
      {  
		  window.clipboardData.setData('text',text);  
      }  
      return false;  
}


//copy code
function copyCode(url,code)  
{ 
openStore(url);
copy(code);
} 

function friend_block_bg(obj)
{
	var liobj = document.getElementById(obj);
	liobj.style.background = "#EFEDED";
	liobj.style.border = "solid 1px #D1D3D4";
}

function friend_block(obj)
{
	var liobj = document.getElementById(obj);
	liobj.style.background = "";
	liobj.style.border = "solid 1px white";
}


function showAction(id) {
	var obj_h = document.getElementById('h_'+id);
	var obj  = document.getElementById('u_'+id);
	obj_h.style.display = "none";
	obj_h.parentNode.style.zIndex = 100;
	obj.style.display = '';				
}
function hideAction(id) {
	var obj_h = document.getElementById('h_'+id);
	var obj  = document.getElementById('u_'+id);
	obj_h.style.display = "";
	obj.style.display = 'none';
	obj_h.parentNode.style.zIndex = 1;
}

function codePre(){
	var x;var y;var tx;
	$().mouseover(function(e){x=e.pageX;y=e.pageY;if(y>=(mouseY+20)||y<=(mouseY-20)){$('#coupon_tooltip').fadeOut(200);};});
	
	$('.codeStyle').hover(function() { //start code mouse over beheavior								   
		var offset=$(this).offset();
		x = offset.left;
		y = offset.top;
		tx = $(this).width()+20;
		if(!$(this).hasClass('codeCut')){
			$('#coupon_tooltip').html('Click for more details');
			$(this).css('background-color','#FF7316');
			$('#coupon_tooltip').css({"left":x+tx+"px","top":y+"px"}).fadeIn(200);
		}
	}, function() {
		if(!$(this).hasClass('codeCut')){
			$(this).css('background-color','#F1EBE0');
			$('#coupon_tooltip').fadeOut(200);
		}
	}); //end code mouse over beheavior
	
	$('.codeStyle').click(function() {//code href action
		var e = this;
		if($(e).attr('tagName') != 'A'){
			var gURL = '/visit/' + $(e).attr('id') + '/0/merchant';
			openStore(gURL);
		}
		return true;
	});//end code href action
}

function codeInit(){
	var clip = null;
	clip = new ZeroClipboard.Client();
	clip.setHandCursor( true );
	var x;var y;var tx;
	$().mouseover(function(e){x=e.pageX;y=e.pageY;if(y>=(mouseY+20)||y<=(mouseY-20)){$('#coupon_tooltip').fadeOut(200);};});
	
	$('.codeStyle.codeCut').mouseover(function (){//start code flash
		var offset=$(this).offset();
		clip.thisId = $(this).attr('id');
		clip.setHandCursor( true );
		clip.setText(this.innerHTML);
		clip.goURL = '/visit/' + this.id + '/0/merchant';
		x = offset.left;
		y = offset.top;
		tx = $(this).outerWidth()+10;
		
		if(clip.div){clip.receiveEvent('mouseout',null);clip.reposition(this);}
		else clip.glue(this);
		clip.receiveEvent('mouseover',null);
		
	});//end code flash	
		
		clip.addEventListener( 'onMouseOver', my_mouse_over_handler );
		clip.addEventListener( 'onMouseOut', my_mouse_out_handler );
		clip.addEventListener( 'onComplete', my_complete );
		
		function my_complete( client ) {
			openStore(clip.goURL);
			$('#'+clip.thisId).css({'background-image':'none','background-color':'#F1EBE0','padding-right':'50px','padding-left':'50px'});
		}	
		
		function my_mouse_over_handler( client ) {
			$('#coupon_tooltip').html('Click to copy &amp; open site');
			$('#coupon_tooltip').css({"left":x+tx+"px","top":y+"px"}).fadeIn(200);
			//alert($("#coupon_tooltip").html());
		}
		
		function my_mouse_out_handler( client ) {
			$('#coupon_tooltip').fadeOut(200);
			clip.hide();
		}	

}

$(function() {
	$('#bdstart').after('<div id="coupon_tooltip"></div>');	
	codePre();
	codeInit();
	
	$('#shareCoupon').click(function (){//start sharecode form set
		if($('#secQestion').is(':hidden')){$('#secQestion').show('slow')}
	});//end sharecode form set
	
	$('.vmcgo').click(function (){//start shop visit
		var gURL = '/visitmc/' + $(this).attr('id') + '/1/merchant';
		openStore(gURL);
		return false;
	});//end shop visit
	
});

function subComment(){
	$("#loading").ajaxStart(function(){
	   $(this).show();
	 });
	$.ajax({ url: "/comment_pro.php",
	 type:"post",           
	 dataType:"html",
	 data: "comment="+$('#comment').val()+"&chkcode="+$('#chkcodeA').val()+"&proid2="+$('#proid2').val()+"&commentBtn="+$('#commentBtn').val(),
	 success:function(msg){
		$("#loading").css("display","none");
	   if(msg!='Thank you! Your comment will be displayed soon.'){
		   $('#chk').html(msg);
		   $('#chkdiv').slideDown();
		}else{
		   $('#chkdiv').css('display','none');
		   $('#ssComent').slideUp();
		   $('#welcome').slideDown();
		}
	 } 
	});
}

function shareCode(){
	$("#scLoading").ajaxStart(function(){
	   $(this).show();
	 });
	$.ajax({ url: "/submit.php",
	 type:"post",           
	 dataType:"html",
	 data: "offer="+$('#offer').val()+"&couponDes="+$('#couponDes').val()+"&exp="+$('#exp').val()+"&mcID="+$('#mcID').val()+"&chkcode="+$('#chkcode').val()+"&submitOffer="+$('#submitOffer').val(),
	 success:function(msg){
		$("#scLoading").css("display","none");
	   if(msg!='Your coupon submitted! thanks! Should show up soon'){
		   $('#rpDes').html(msg);
		}else{
		   $('#rpDes').html(msg);
		   $('#form_show').slideUp();
		}
	 } 
	});
}



