//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; 
                     } 
            } 
    } 
}

function wsug(e, str){
 var oThis = arguments.callee;
 if(!str) {
  oThis.sug.style.visibility = 'hidden';
  document.onmousemove = null;
  return;
 }  
 if(!oThis.sug){
  var div = document.createElement('div'), css = 'top:0; left:0; position:absolute; z-index:100; visibility:hidden';
   div.style.cssText = css;
   div.setAttribute('style',css);
  var sug = document.createElement('div'), css= 'font:normal 12px/16px "Arial"; white-space:nowrap; color:#000; padding:3px; position:absolute; left:0; top:0; z-index:10; background:#f9fdfd; border:1px solid #0aa';
   sug.style.cssText = css;
   sug.setAttribute('style',css);
  var dr = document.createElement('div'), css = 'position:absolute; top:3px; left:3px; background:#333; filter:alpha(opacity=50); opacity:0.5; z-index:9';
   dr.style.cssText = css;
   dr.setAttribute('style',css);
  var ifr = document.createElement('iframe'), css='position:absolute; left:0; top:0; z-index:8; filter:alpha(opacity=0); opacity:0';
   ifr.style.cssText = css;
   ifr.setAttribute('style',css);
  div.appendChild(ifr);
  div.appendChild(dr);
  div.appendChild(sug);
  div.sug = sug;
  document.body.appendChild(div);
  oThis.sug = div;
  oThis.dr = dr;
  oThis.ifr = ifr;
  div = dr = ifr = sug = null;
 }
 var e = e || window.event, obj = oThis.sug, dr = oThis.dr, ifr = oThis.ifr;
 obj.sug.innerHTML = '<img src="' + str + '" border="0">';
 
 var w = obj.sug.offsetWidth, h = obj.sug.offsetHeight, dw = document.documentElement.clientWidth||document.body.clientWidth; dh = document.documentElement.clientHeight || document.body.clientHeight;
 var st = document.documentElement.scrollTop || document.body.scrollTop, sl = document.documentElement.scrollLeft || document.body.scrollLeft;
 var left = e.clientX +sl +17 + w < dw + sl  &&  e.clientX + sl + 15 || e.clientX +sl-8 - w, top = e.clientY + st +17 + h < dh + st  &&  e.clientY + st + 17 || e.clientY + st - 5 - h;
 obj.style.left = left+ 10 + 'px';
 obj.style.top = top + 10 + 'px';
 dr.style.width = w + 'px';
 dr.style.height = h + 'px';
 ifr.style.width = w + 3 + 'px';
 ifr.style.height = h + 3 + 'px';
 obj.style.visibility = 'visible';
 document.onmousemove = function(e){
  var e = e || window.event, st = document.documentElement.scrollTop || document.body.scrollTop, sl = document.documentElement.scrollLeft || document.body.scrollLeft;
  var left = e.clientX +sl +17 + w < dw + sl  &&  e.clientX + sl + 15 || e.clientX +sl-8 - w, top = e.clientY + st +17 + h < dh + st  &&  e.clientY + st + 17 || e.clientY + st - 5 - h;
  obj.style.left = left + 'px';
  obj.style.top = top + 'px';
 }
}

//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 desBk(longDesc,strLen){
	var prefix = '&nbsp;<a href=\'javascript:void(0)\' onclick=\'desExp(&quot;'+longDesc+'&quot;,'+strLen+')\'>Hide</a>';
	$('#' + longDesc).html(oriInfo + prefix);
}

function desExp(longDesc,strLen){
	var word_length = (strLen) ? strLen : 200;
	var mcInfo = $('#' + longDesc).html();
	var prefix = '<a href=\'javascript:void(0)\' onclick=\'desBk(&quot;'+longDesc+'&quot;,'+strLen+')\'>expand</a>';
	var string = mcInfo;
	if (word_length > string.length) return;
	var pos = word_length;
	while (pos < string.length) {
		var w = string.slice(pos, pos+1);
		if (w == ' ') break;
		pos++;
	}
	mcInfo = string.slice(0, pos) + ' <span class="conTruncated">' + string.slice(pos, string.length) + '</span> ... ' + prefix;
	$('#' + longDesc).html(mcInfo);
}
			
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);
		}
		
		function my_mouse_out_handler( client ) {
			$('#coupon_tooltip').fadeOut(200);
			clip.hide();
		}	

}

function addUrlLk(){
	$('.disDescDisplay').each(function(){
		var rawID = $(this).attr('id');
		var goID = rawID.substring(7);
		var rawPre = rawID.substring(0,6);
		var oldContent = $(this).html();
		if(rawPre=="disPid"){
			var outLink = "/visit/" + goID + "/0/merchant";	
		}else{
			var outLink = "/visitmc/" + goID + "/0/merchant";	
		}
		if(oldContent.indexOf("<a href=")<0){
			var newContent = '<a href="' + outLink + '" target="_blank">' + oldContent + '</a>';
			$(this).html(newContent);
		}
		
	});
}

$(function() {
	$('#bdstart').after('<div id="coupon_tooltip"></div>');	
	codePre();
	codeInit();
	
	$('#shareCoupon').click(function (){//start sharecode form set
		if ($("#secQestion").length<=0) {
			$('#scodeExpDate').after('<div id="secQestion" style="display:none;"><p><b>Security Question:</b><br><img src="/include/safecode.php?Action=Reg&imgW=60&imgH=30" alt="Security Question" width="60px" height="30px" style="vertical-align:middle;">&nbsp;&nbsp;=&nbsp;&nbsp;<input class="input" style="width: 60px;" maxlength="6" id="chkcode" name="chkcode"><br><span class="font-abs">For security, please type the sum of numbers above</span></p></div>');
		}
		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
	
	if ($("#longDesc").length>0) {
		desExp('longDesc',230);
	}
	
	$("#mcRtCouponsL").css("display","none"); // merchant related coupons
	$("#expCouponsL").css("display","none"); // merchant expired coupons
	
	addUrlLk();//add mc coupons links
			
});


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();
		}
	 } 
	});
}

function subReview(){
	$.ajax({ url: "/subReview.php",
	 type:"post",           
	 dataType:"html",
	 global: false,
	 data: "uName="+encodeURIComponent($('#uName').val())+"&reviewDes="+encodeURIComponent($('#reviewContent').val())+"&rt="+$(".sReview:checked").val()+"&mcID="+$('#rmcID').val(),
	 success:function(msg){
	   if(msg!='Your review submitted! thanks! Should show up soon'){
		   $('#rwDes').html(msg);
		}else{
		   $('#rwDes').html(msg);
		   $('#divRw').slideUp();
		}
	 } 
	});
}





