function formatItem(row) {
	return row[0];
}

function closeInputErrors(){
	$('.inputerror').hide();
}

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};

function updateDeals(nums,crits,crids,startn,typet,putin) {
	$.get('/sys_inc/ajax_res.php?delajax=1',
		{num:nums,crit:crits,crid:crids,start:startn,type:typet},
		function(data){
			$('#'+putin).html(data);
		});
}

function isPlaceholderSupported() {
	var input = document.createElement("input");
	return ('placeholder' in input);
}

//$(document).ready(function () {
$(window).load(function(){
		
	$("#delv").on("click", "span.ratelink", function() {
		var dargs = $(this).attr('rel').split(",");
		if (dargs[1] == 1) set = 'p';
		else set = 'm';
		$.get('/sys_inc/ajax_res.php', {rater2:1,itemid:dargs[0],rating:dargs[1],type:dargs[2]}, function(data){
			var resps = data.split("|");
			if (resps[0] == 1) {
				$('#stat'+dargs[0]).html(resps[2]).show().delay(2000).hide("slow");
				$('#'+dargs[2]+set+dargs[0]).html(resps[1]);
			} else if(resps[0] == 0) {
				$('#stat'+dargs[0]).html(resps[2]).show().delay(10000).hide("slow");
				$('#'+dargs[2]+set+dargs[0]).html(resps[1]);
			} else {
				$('#stat'+dargs[0]).html(resps[2]).show().delay(2000).hide("slow");				
			}
		});
	});	
	
	$("#delv").on("click", "span.commentlink", function() {
		var dargs = $(this).attr('rel').split(",");
		var commdiv = $(this).parents('.dealtabs').next();
		commdiv.slideFadeToggle();
		if(commdiv.text().indexOf('Loading Comments') > -1) {
			commdiv.load('/sys_inc/ajax_res.php?showcomms=1&itemid='+dargs[0]+'&itemtype='+dargs[1]);
		}
	});

	$('#bfan').click(function() {
		var dargs = $(this).attr('rel').split(",");
		$.get("/sys_inc/ajax_res.php", { bfan: dargs[0], fanid: dargs[1], starid: dargs[2] },
		function(data){
			if (data.indexOf("You") == 0) {
				$('#fanres').show().text(data);
			} else {
				$('#fanres').show().text("You have been added to this "+dargs[0]+"'s followers.");
				$('#nofan').hide();
				$('#newfan').show().html(data);
			}
		});
	});

	$('.delmen li').click(function() {
		$('#tloader').css('display', 'inline-block');
		var dargs = $(this).attr('rel').split(",");
		$.get('/sys_inc/ajax_res.php?delajax=1',
			{num:dargs[0],crit:dargs[1],crid:dargs[2],start:dargs[3],type:dargs[4]},
			function(data){
				$('#delv').html(data);
				$('#tloader').hide();
			});	
		//updateDeals(dargs[0],dargs[1],dargs[2],dargs[3],dargs[4],"delv");
		$(this).siblings().removeClass("selected");
		$(this).addClass("selected");
	});

	$("#delv").on("click", ".nextr", function() {
		$(this).remove();
		$('#dloader').show();
		var dargs = $(this).attr('rel').split(",");
		$.get('/sys_inc/ajax_res.php?delajax=1',
			{num:dargs[0],crit:dargs[1],crid:dargs[2],start:dargs[3],type:dargs[4]},
			function(data){
				$('#delv').append(data);
				$('#dloader').remove();
			});
		
	});
 
	var plcsup = isPlaceholderSupported();
	
	$('#sgnform input[placeholder]').focus(function() {
	  var input = $(this);
	  $('.lgnhidden').show();
	  if (input.val() == input.attr('placeholder')) {
	    if (this.originalType && plcsup) {
	      this.type = this.originalType;
	      delete this.originalType;
	    }
	    input.val('');
	    input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '') {
	    if (this.type == 'password' && plcsup) {
	      this.originalType = this.type;
	      this.type = 'text';
	    }
	    input.addClass('placeholder');
	    input.val(input.attr('placeholder'));
	    $('.lgnhidden').hide();
	  }
	}).blur();
	
	$('.date-pick').dateinput({format:'mm/dd/yyyy',min:-1}).click(function() {
		if($(this).val() == 'MM/DD/YYYY')	$(this).val('');
	});
	
	$('#qsearch').focus(function() {
		if($(this).val() == 'type \'books\' or \'Newegg\' and hit enter...') $(this).val('');
	}).blur(function() {
		if($(this).val() == '' || $(this).val() == '') $(this).val('type \'books\' or \'Newegg\' and hit enter...');
	});
	
	$('#quick-search').submit(function() {
		if($('#qsearch').val() == '') return false;
		else return true;
	});
	
	$("#sgnform").validator({
		singleError: true,
		messageClass: 'inputerror',
		position: 'bottom center'
	}).submit(function(e) {
		if (!e.isDefaultPrevented()) {
			var stat = $('#loginres');
			var ffields = $('#sgnform');
			var sgnerror = false;
			$(this).find('[placeholder]').each(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					sgnerror = true;
				}
			});	
			if (!sgnerror) {
				$(this).ajaxSubmit({
			        success: function(responseText) {
			        	stat.html(responseText);
			        },
					beforeSubmit: function() {
						ffields.hide();
						stat.html("<img src=\"/inc/images/ajax-loader.gif\" /> Submitting. Please Wait...").show();
					}
				});
			}
			return false;
		}
	});
	
	$(".myform").validator({
		singleError: true,
		messageClass: 'inputerror',
		position: 'bottom center'
	}).submit(function(e) {
		if (!e.isDefaultPrevented()) {
			var stat = $('.cpnstat',this);
			var ffields = $('.cpnfield',this);
			$(this).ajaxSubmit({
		        success: function(responseText) {
		        	stat.html(responseText);
		        },
				beforeSubmit: function() {
					ffields.slideFadeToggle();
					stat.html("<img src=\"/inc/images/ajax-loader.gif\" /> Submitting. Please Wait...").slideFadeToggle();
				}
			});
			return false;
		}
	});
	
	$("a[id^='cz'],a[id^='cx']").click(function(){
		var sid = $(this).attr("id").substring(2);
		var ccode = $("#cx"+sid).attr("rel");
		var linkr = 'http:\/\/'+location.hostname+$(this).attr('href');
		if ($("#cx"+sid).text() == "Click to View") $("#cx"+sid).hide().text(ccode).fadeIn("slow");
		window.open(linkr,'_blank');
		self.focus();
	});
	
	$("span[id^='cc']").zclip({
		path:'sys_inc/ZeroClipboard.swf',
		copy:function(){return $(this).attr("rel");},
		afterCopy:function(){
			var sid = $(this).attr("id").split('|');
			$('#stat'+sid[1]).html("Coupon code copied to clipboard! :)").show().delay(2000).hide("slow");
		}
	});
	
	$("#addni,#addcpnlbl,#adddellbl").click(function() {
		$("#addnif").slideFadeToggle("slow");
	});

	$("#addnis").autocomplete(
		"/sys_inc/ajax_res_stores.php",
		{
			delay:10,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			formatItem:formatItem,
			autoFill:false
		}
	);

	$("#addnis").result(function(event, data, formatted) {
		var sValue = data[1];
		$("#storeid").val(sValue);
	});

	if (screen.width<800) $('.modal').css({"width":"270px"});

	$(".signin").overlay({
		top: 80,
		speed: 0,
		mask: {
			color: '#EEEEEE',
			loadSpeed: 0,
			opacity: 0.4
		},
		onBeforeClose: closeInputErrors
	});

	$('.captchaImage').click(function(){
		$(this).siblings().removeClass("simpleCaptchaSelected");
		$(this).addClass("simpleCaptchaSelected");
		$(this).parent().next().val($(this).attr('rel'));
	});
	
	$('.sel3').bind('change', function(){ if (this.options[this.selectedIndex].value) document.location = this.options[this.selectedIndex].value; });
	
});
