var agt=navigator.userAgent.toLowerCase();
var ie  = (agt.indexOf("msie") != -1);
var ns  = (navigator.appName.indexOf("Netscape") != -1);
var win = ((agt.indexOf("win")!=-1) || (agt.indexOf("32bit")!=-1));
var mac = (agt.indexOf("mac")!=-1);

function detectInternetExplorer(ClassID,name) { 
	result = false; document.write('<SCRIPT LANGUAGE=VBScript>\n on error resume next \n result = IsObject(CreateObject("' + ClassID + '"))</SCRIPT>\n'); if (result) return name+','; else return ''; 
}

function detectNS(ClassID,name) { 
	n = ""; 
	if (nse.indexOf(ClassID) != -1) if (navigator.mimeTypes[ClassID].enabledPlugin != null) n = name+","; 
	return n; 
}

if (ie && win) {
	
	pluginlist = detectInternetExplorer("Adobe.SVGCtl","SVG Viewer");
	pluginlist += detectInternetExplorer("SWCtl.SWCtl.1","Shockwave Director");
	pluginlist += detectInternetExplorer("ShockwaveFlash.ShockwaveFlash.1","Shockwave Flash");
	/*
	since we dont want ot check other plugins.....we only need to check flash 
	pluginlist += detectInternetExplorer("rmocx.RealPlayer G2 Control.1","RealPlayer");
	pluginlist += detectInternetExplorer("QuickTimeCheckObject.QuickTimeCheck.1","QuickTime") 
	pluginlist += detectInternetExplorer("MediaPlayer.MediaPlayer.1","Windows Media Player") 
	pluginlist += detectInternetExplorer("PDF.PdfCtrl.5","Acrobat Reader");
	*/ 
}

if (ns || !win) {
		nse = ""; for (var i=0;i<navigator.mimeTypes.length;i++) nse += navigator.mimeTypes[i].type.toLowerCase();
		pluginlist = detectNS("image/svg-xml","SVG Viewer"); 
		pluginlist += detectNS("application/x-director","Shockwave Director"); 
		pluginlist += detectNS("application/x-shockwave-flash","Shockwave Flash"); 
		/*	
		since we dont want ot check other plugins.....we only need to check flash 
		pluginlist += detectNS("audio/x-pn-realaudio-plugin","RealPlayer");
		pluginlist += detectNS("video/quicktime","QuickTime");
		pluginlist += detectNS("application/x-mplayer2","Windows Media Player");
		pluginlist += detectNS("application/pdf","Acrobat Reader");
		*/
}

pluginlist += navigator.javaEnabled() ? "Java," : "";

if (pluginlist.length > 0) pluginlist = pluginlist.substring(0,pluginlist.length-1);

//SAMPLE USAGE- detect "Flash"
//if (pluginlist.indexOf("Flash")!=-1)
//document.write("You have flash installed")
if (pluginlist.indexOf('Flash') == -1 ||  navigator.platform == 'ipad') {
	var FlashSupport = false;
	
} else {
	var FlashSupport = true;
}


var clip = null;
var link = null;
var revealUrl = null;
var current_id = null;

clip = new ZeroClipboard.Client();
if (FlashSupport) {

	clip.setHandCursor( true );
} else {
	clip.setHandCursor( false );
}
	
	//clip.addEventListener('mouseup', function(client){ popup(link);});
	
clip.addEventListener('mouseup', function(client){ popupNew(link,revealUrl);});


clip.addEventListener( 'mouseOut', function(client) { 
	hide(current_id);
});
	
clip.addEventListener('mouseOver', function(client) {
	show(current_id);
});


var CurrentSelectedCouponContainer = null;
clip2 = new ZeroClipboard.Client();
if (FlashSupport) {
	clip2.setHandCursor( true );
} else {
	clip2.setHandCursor( false );
}


//clip.addEventListener('mouseup', function(client){ popup(link);});
clip2.addEventListener('mouseup', function(client){ changeHintText(current_id);});


clip2.addEventListener( 'mouseOut', function(client) { 
    if (CurrentSelectedCouponContainer != current_id) {
	hide(current_id);
    }
});
    
clip2.addEventListener('mouseOver', function(client) {
	if (CurrentSelectedCouponContainer != current_id) {
		show(current_id);
	}
});


function changeHintText(id){
	var allelm = document.querySelectorAll('div.redeem_coupon_code_hint');
	CurrentSelectedCouponContainer = id;	
	for(i=0; i<allelm.length; i++){
		//alert(allelm[i].innerHTML);
		if (allelm[i].id != id) {
			allelm[i].style.display = 'none';
			allelm[i].innerHTML = 'Click to copy to clipboard';
		}			
	}
	document.getElementById(id).innerHTML='Code copied to your clipboard';
	document.getElementById(id).style.display = 'block';
	return;
}

function popup(link){ 
	window.open(link);
}

function popupNew(link,revealUrl){
	
	if (navigator.platform.toLowerCase() == 'ipad')
	{
		if (window.opener == null) {
			var altproperty = '_blank';
		} else {
			var altproperty = '_new';
		}
	} 
	else if (navigator.userAgent.toLowerCase().indexOf('safari') != -1 && navigator.userAgent.toLowerCase().indexOf('chrome') < 0) {
		if (window.opener == null) {
			var altproperty = '_blank';
		} else {
			var altproperty = '_new';
		}
		var merchantUrl = link;
		link = revealUrl;
		revealUrl = merchantUrl;
		//var altproperty = '_new';
	}
	else {
		var altproperty = '_blank';
	}
	
	newWindow = window.open(revealUrl, altproperty, '', false);
	newWindow.focus();
	window.location.href=link;
	
}

function ctc(text, hyperlink, object){
	
	clip.setText(text);
	if (clip.div){
		clip.receiveEvent('mouseout', null);
		clip.reposition(object);
	}
	else clip.glue(object);
    
    clip.receiveEvent('mouseout', null);
	
	link = hyperlink;
};

function ctcNew(text, hyperlink, object,RevealUrl){
	
	if (FlashSupport)
	{
		clip.setText(text);
		if (clip.div){
			clip.receiveEvent('mouseout', null);
			clip.reposition(object);
		}
		else clip.glue(object);
		
		clip.receiveEvent('mouseout', null);
		revealUrl = RevealUrl;
		link = hyperlink;

	} else {
		
		object.addEventListener('click',function(){ popupNew(hyperlink,RevealUrl);});
		//popupNew(hyperlink,RevealUrl);
	}
	//return;
};

function ctcOnlyCopy(text, object){
	
	if (FlashSupport)
	{
		clip2.setText(text);
		if (clip2.div){
			clip2.receiveEvent('mouseout', null);
			clip2.reposition(object);
		}
		else clip2.glue(object);
		
		clip2.receiveEvent('mouseout', null);
		//revealUrl = RevealUrl;
		//link = hyperlink;

	} else {
		//object.addEventListener('mouseover',showhintdiv);
		//object.addEventListener('mouseout',hidehintdiv);
	} 
	
	//return;
};

function show(id) {
	document.getElementById(id).style.visibility='visible';
	document.getElementById(id).style.display='block';
}

function hide(id) {
	document.getElementById(id).style.visibility='hidden';
	document.getElementById(id).style.display='none';
}

function showhintdiv() {
	document.getElementById(current_id).style.visibility='visible';
	document.getElementById(current_id).style.display='block';
}

function hidehintdiv() {
	document.getElementById(current_id).style.visibility='hidden';
	document.getElementById(current_id).style.display='none';
}


function openmerchant(revealUrl,merchantUrl) {
	//alert(revealUrl+' \n\n '+merchantUrl);
	var newWindow = window.open(merchantUrl, '_blank', '', false);
	window.location.href=revealUrl;
	newWindow.opener.focus();
	//window.focus();
}



