// JavaScript Document
var clip = null;
var intcnt;
var blinkCnt = 0;
var flag = 0;
var goblink = 1;

$(document).ready(function(){
	init();
	
	if($("#selectStore"))
	{
		$("#selectStore").autocomplete(
			"/ajx-suggestions.php?for=store",
			{
				delay:5,
				minChars:1,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				onItemSelect:function(li){ findStrValue(li); },
				onFindValue:findStrValue,
				formatItem:function(row){ return row[0]; },
				autoFill:false
			}
		);
	}
	
	if($("#selectStore1"))
	{
		$("#selectStore1").autocomplete(
			"/ajx-suggestions.php?for=store",
			{
				delay:5,
				minChars:1,
				matchSubset:1,
				matchContains:1,
				cacheLength:10,
				onItemSelect:function(li){ findStrValue(li); },
				onFindValue:findStrValue,
				formatItem:function(row){ return row[0]; },
				autoFill:false
			}
		);
	}
	
	$("#selectStore").focus(function(){
		if(Trim($(this).val()) == 'Search Coupon Codes By Store Name...')
		{
			$(this).attr('value','');
		}
	}).blur(function(){
		if(Trim($(this).val()) == '')
		{
			$(this).attr('value','Search Coupon Codes By Store Name...');
		}
	});
	
	$("#selectStore1").focus(function(){
		if(Trim($(this).val()) == 'Search Coupon Codes By Store Name...')
		{
			$(this).attr('value','');
		}
	}).blur(function(){
		if(Trim($(this).val()) == '')
		{
			$(this).attr('value','Search Coupon Codes By Store Name...');
		}
	});
	
	if($('#cpn-str-name'))
	{
		var p = $('#cpn-str-name');
		if(p.position())
		{
			var position = p.position();
	
			$('html, body').animate({
				scrollTop: position.top
			}, 1000);
		}
	}
});

function findStrValue(li) {
	if( li == null ) return alert("No match!");
}

function getCouponsByStore(loadDiv,mainDiv)
{
	var passdata = '';
	if(Trim($('#selectStore').val()) != '' && Trim($('#selectStore').val()) != 'Search Coupon Codes By Store Name...')
	{
		passdata += 'keyword='+escape($('#selectStore').val());
	}
	
	if(Trim(passdata) != '')
	{
		$('#'+loadDiv).html('<div><img src="/images/loader_small.gif" /></div>');
		$.ajax({
			url: '/ajx-coupon-search.php',
			type: 'POST',
			data: passdata,
			success: function(data){
				var tempdata = Trim(data);
				var temp1 = tempdata.split('@@@@@');
				if(temp1[0] == 'REDIRECT')
				{
					window.location = temp1[1];
				}
				else
				{
					$('#'+loadDiv).html('Browse Coupon Codes');
					$('#'+mainDiv).html(data);
				}
			}
		});
	}
	else
	{
		$('#'+loadDiv).html('Please enter store name');
		$('#selectStore').focus();
	}
}

function getCouponsByStore1(loadDiv,mainDiv)
{
	var passdata = '';
	if(Trim($('#selectStore1').val()) != '' && Trim($('#selectStore1').val()) != 'Search Coupon Codes By Store Name...')
	{
		passdata += 'keyword='+escape($('#selectStore1').val());
	}
	
	if(Trim(passdata) != '')
	{
		$('#'+loadDiv).html('<div><img src="/images/loader_small.gif" /></div>');
		$.ajax({
			url: '/ajx-coupon-search.php',
			type: 'POST',
			data: passdata,
			success: function(data){
				var tempdata = Trim(data);
				var temp1 = tempdata.split('@@@@@');
				if(temp1[0] == 'REDIRECT')
				{
					window.location = temp1[1];
				}
				else
				{
					$('#'+loadDiv).html('Browse Coupon Codes');
					$('#'+mainDiv).html(data);
					
					// Scroll to results
					var p = $('#selectStore');
					if(p.position())
					{
						var position = p.position();
				
						$('html, body').animate({
							scrollTop: position.top - 70
						}, 1000);
					}
					
					$('#selectStore').attr('value',$('#selectStore1').val());
					$('#selectStore1').attr('value','Search Coupon Codes By Store Name...');
				}
			}
		});
	}
	else
	{
		$('#'+loadDiv).html('Please enter store name');
		$('#selectStore1').focus();
	}
}

function save_cpn(cpnid,opt)
{
	if(opt == 1)
	{
		$('#cpn-save'+cpnid).slideUp();
		$('#cpn-save-target'+cpnid).slideDown();
	}
	else if(opt == 0)
	{
		$('#cpn-save-target'+cpnid).slideUp();
		$('#cpn-save'+cpnid).slideDown();
	}
	else if(opt == 2)
	{
		var value1 = parseInt(Trim($('#text_save1_'+cpnid).val()));
		var value2 = parseInt(Trim($('#text_save2_'+cpnid).val()));
		value1 = (value1 > 0) ? value1 : 0;
		value2 = (value2 > 0) ? value2 : 0;
		var saved_val = value1+'.'+value2;
		var passdata = 'save_amount='+saved_val+'&coupon='+cpnid;
		
		if(value1 > 0 || value2 > 0)
		{
			$('#cpn-save-target'+cpnid).html('<div><img src="/images/loader_small.gif" /></div>');
			$.ajax({
				url: '/ajx-coupon-search.php',
				type: 'POST',
				data: passdata,
				success: function(data){
					$('#cpn-save-target'+cpnid).html('');
					if(Trim(data) != '')
					{
						goblink = 0;
						setLikeCounter(cpnid,'1');
						$('#cpn-save'+cpnid).html('<div><b>Thanks for sharing information with us. We appreciate that. <a href="javascript:void(0)" onclick="showHistory(\''+cpnid+'\',\'\',\'cpn-save'+cpnid+'\')">View Details</a></b></div>');
						$('#cpn-save-target'+cpnid).slideUp();
						$('#cpn-save'+cpnid).slideDown();
					}
					else
					{
						$('#cpn-save-target'+cpnid).slideUp();
						$('#cpn-save'+cpnid).slideDown();
					}
				}
			});
		}
		else
		{
			$('#cpn-save-target'+cpnid).slideUp();
			$('#cpn-save'+cpnid).slideDown();
		}
	}
}

function setLikeCounter(cpnid,val)
{
	var passdata = 'id='+cpnid+'&val='+val;
	if(passdata != '')
	{
		$('#cpn-like'+cpnid).html('<div><img src="/images/loader_small.gif" /></div>');
		$.ajax({
			url: '/ajx-like-cmn.php',
			type: 'POST',
			data: passdata,
			success: function(data){
				$('#cpn-like'+cpnid).html('');
				if(Trim(data) != '')
				{
					$('#cpn-like'+cpnid).html('<div><b>Thanks for your opinion.</b></div>');
					$('#cpn-succ'+cpnid).html(data);
					
					if(val == 1 && goblink == 1)
					{
						blinkCnt=0;
						intcnt = setTimeout("blinkDiv('"+cpnid+"')",200);
					}
					else
					{
						goblink = 1;
					}
				}
			}
		});
	}
}

function blinkDiv(cpnid)
{
	if(blinkCnt < 10)
	{
		if(flag == 0)
		{
			flag = 1;
			$('#cpn-save'+cpnid).hide();
			$('#cpn-save-target'+cpnid).show();
		}
		else
		{
			flag = 0;
			$('#cpn-save-target'+cpnid).hide();
			$('#cpn-save'+cpnid).show();
		}
		blinkCnt++;
		intcnt = setTimeout("blinkDiv('"+cpnid+"')",200);
	}
	else
	{
		clearTimeout(intcnt);
		blinkCnt=11;
		save_cpn(cpnid,1);
	}
}

function showHistory(cpnid,storeid,divid)
{
	if(storeid != '')
	{
		if($('#'+divid).is(':hidden'))
		{
			var passdata = 'cpnid='+cpnid+'&storeid='+storeid+'&divid='+divid;
			if(passdata != '')
			{
				$('#'+divid+'-text').html('Close');
				$.ajax({
					url: '/str-cpn-history.php',
					type: 'POST',
					data: passdata,
					success: function(data){
						if(Trim(data) != '')
						{
							$('#'+divid).html(data);
							$('#'+divid).slideDown("slow");
						}
					}
				});
			}
		}
		else
		{
			$('#'+divid+'-text').html('View');
			$('#'+divid).slideUp("slow");
		}
	}
	else
	{
		var passdata = 'cpnid='+cpnid+'&storeid='+storeid+'&divid='+divid;
		if(passdata != '')
		{
			$('#'+divid).html('<img src="/images/loader_small.gif" />');
			$.ajax({
				url: '/str-cpn-history.php',
				type: 'POST',
				data: passdata,
				success: function(data){
					if(Trim(data) != '')
					{
						$('#'+divid).html(data);
					}
				}
			});
		}
	}
}

function setFeature()
{
	if($('#feat-str').is(":hidden"))
	{
		$('#feat-str').slideDown();
		$('#ftr-img')[0].src = '/images/spl-ofr-uparrow.gif';
		$('#ftr-img')[0].title = 'Hide Stores';
		Set_Cookie('ftr', 1, 365);
	}
	else
	{
		$('#feat-str').slideUp();
		$('#ftr-img')[0].src = '/images/spl-ofr-downarrow.gif';
		$('#ftr-img')[0].title = 'Show Stores';
		Set_Cookie('ftr', 0, 365);
	}
}

function dataCopiedNow(msg)
{
	document.getElementById(FinaldivIs).innerHTML  = msg;
}
function CopyMsgShow(par1)
{
	document.getElementById(par1).style.display='inline';
}	
function CopyMsgHide(par1)
{
	document.getElementById(par1).style.display='none';
}
			
function init() {
	// setup single ZeroClipboard object for all our elements
	clip = new ZeroClipboard.Client();
	clip.setHandCursor( true );
	//alert($('div.ccode'));
	// assign a common mouseover function for all elements using jQuery
	$('div.ccode span').mouseover( function() {
		// set the clip text to our innerHTML
		//alert(this.innerHTML);
		var movId = '';
		movId = this.dsd;
		//CopyMsgShow(movId);
		//alert(this.dsd);
		clip.setText( this.innerHTML );
		
		// reposition the movie over our element
		// or create it if this is the first time
		if (clip.div) {
			clip.receiveEvent('mouseout', null);
			clip.reposition(this);
		}
		else clip.glue(this);
		
		// gotta force these events due to the Flash movie
		// moving all around.  This insures the CSS effects
		// are properly updated.
		clip.receiveEvent('mouseover', null);
	} );
	
	$('div.ccode span').mouseout( function() {
		// set the clip text to our innerHTML
		//alert(this.innerHTML);
		var movId1 = '';
		movId1 = this.dsd;
		//CopyMsgHide(movId1);
		//alert(this.dsd);
	} );
}