/* $Id: main.js 445 2008-12-12 22:13:58Z vincent $ */

$(document).ready(function(){
	
	$('#checkun').click(function(){
		$("input:checkbox").each(function(){
			var checked_status = $(this).is(':checked');
			if (checked_status == false) {checked_status = true; $("#checkun").attr("value", "Uncheck All")}
			else{checked_status = false;  $("#checkun").attr("value", "Check All")};
			$(this).attr("checked", checked_status);
		});
	});
	
	$('#deletesl').click(function(){
		$("#confirmdelete").dialog({
			resizable: false,
			height:140,
			width:350,
			modal: true,
			buttons: {
				Delete: function() {
					$(this).dialog('close');
					$(this).dialog('destroy');
					$("input:checkbox").each(function(){
					  var coupon = $(this).attr("value");
						var item = $(this).parent().parent().attr("id").substring(5);
					  if ($(this).is(':checked')) {
						$($(this).parent().parent()).animate({ backgroundColor: "#fbc7c7" }, "fast").animate({ opacity: "hide" }, "slow");
					    console.log(coupon, item);
					    $.ajax({
			          url: "/shoppinglist/delete/coupon/"+coupon+"/item/" + item,
			         // data: "coupon="+3+"&item=" + 4,
			          cache: false,
			          success: function(html){
			            console.log(html);
			            //resultField.html(html);
			           // resultField.show();
			          }
			        });
					  }
					});
					
				},
				Cancel: function() {
					$(this).dialog('close');
					$(this).dialog('destroy');
					return false
				}
			},
			title: 'Comfirm Action',
			position: ["top", 100],
			zIndex: 30000000
		});
		
	});
	
	
	var closePop = function() {
		$(".closepopup").click(function(){
			$(".nc_popup").hide();
			$("#coupBackgroundPopup").hide("fast");
		});
	}
	
	var bindBehaviors = function() {
		$("#coupons_table tbody tr .trClick").click(function (e) {
			$(".nc_popup").hide();
			$("#coupBackgroundPopup").show();
			var couponId = $(this).parent().attr("id");
			$.get("/coupons/popup", { couponid: couponId },
			function(data){
				var xPos = e.pageX;
				var yPos = e.pageY;
				$('#wrapper').append('<div class="nc_popup" style="position:absolute;top:' + yPos + 'px; left:'+xPos+'px;">' + data + '</div');
				closePop();
				$("#coupBackgroundPopup").click(function () {
					$(".nc_popup").hide();
					$("#coupBackgroundPopup").hide("fast");
				});
			});
		});
	 }

	var bindBehaviorsMatched = function() {
		$("#coupon_matchup #coupons_table tr .action input:checkbox").click(function (e) {
			var target = $("#matches");
			var checkbox = $(this).attr("value");
			var isChecked = $(this).attr("checked");
			var tmpDiv = $("#coupon_matchup #coupons_table #" + checkbox);
			if (isChecked) {
				tmpDiv.effect("transfer", { to: target }, 500, function(){
					if (target.attr('value') == '')  {
						target.attr('value', checkbox);
					}

					else {
						var targetValue = target.attr("value") + ', ' + checkbox;
						target.attr('value', targetValue);
					}
				});
			}
			
			else {
				var replace = new RegExp(",*\s*" + checkbox + ",*\s*", "g");
				var myNewString = target.attr('value').replace(replace, '');

				target.effect("transfer", { to: $(this) }, 500, function(){
					target.attr('value', myNewString);
				});
			}
		});
	}
	
	bindBehaviorsMatched();
	bindBehaviors();
	
	$("#matchup_btn").click(function(){
		var strData = $("#matchupForm").serialize();
		var ajaxPost = $("#matchupForm").attr("action");
		$.ajax({
			type: "POST",
			url: ajaxPost,
			cache: false,
			data: strData + '&action=matchup',
			success: function(data){
				$("#test").html(data);
				if (data == 'OK') {
					resetForm("matchupForm");
					resetInput(".action input");
					$("#matchesalert").html("Your product and coupon were matched.");
				}
				else { $("#matchesalert").html(data); }
				
				$("#matchesalert").dialog({
							resizable: false,
							height:140,
							modal: true,
							overlay: {
								backgroundColor: '#000',
								opacity: 0.5
							},
							buttons: {
								Ok: function() {
									$(this).dialog('close');
									$(this).dialog('destroy');
								}
							},
							title: 'Matchup Coupons',
							position: ["top", 40],
							zIndex: 30000000
				});
			}
		});
	});

	// Coupons Filters select
	$(".couponfilters").bind("change", function(){
		var strData = $("#filterCouponSearch").serialize();
		$.post("/coupons/", strData,
		function(data){
			$("#coupon_left").html(data);
			bindBehaviors();
		});
	});
	
	// Coupons filters auto search - removed
	$("#keywords_removedfornow").live("keyup", function(){
		var strData = $("#keywords").serialize();
		$.post("/coupons/matchups-search", strData,
		function(data){
			$("#coupons_table").html(data);
			bindBehaviors();
			bindBehaviorsMatched();
		});
	});
	
	// Coupons filters button search - active
	$("#submit_coupon_matchup").click(function(){
		var strData = $("#keywords").serialize();
		$.get("/coupons/matchups-search", strData,
		function(data){
			$("#coupons_table").html(data);
			bindBehaviors();
			bindBehaviorsMatched();
		});
	});
	
	// Coupons filters Clear form
	$("#reset_coupon_matchup").click(function(){
		$("#keywords").attr("value", "");
		var defaultMatch = '<thead><tr>';
		defaultMatch += '<th scope="col" id="description">Description</th><th scope="col" id="value">Value</th>';
		defaultMatch += '<th scope="col" id="type">Type</th><th scope="col" id="expires">Expires</th><th scope="col" id="action">Select</th>';
		defaultMatch += '</tr></thead>';
		defaultMatch += '<tbody><tr><td colspan="6">Please Enter Some Search Criteria</td></tr></tbody>';
		$("#coupons_table").html(defaultMatch);
	});

	$("#filterCouponSearch #reset").click(function(){
		var loc = window.location= '/coupons';
		resetForm("filterCouponSearch");
		$.get("/coupons/", function(data){
			$("#coupon_left").html(data);
			bindBehaviors();
			return false;
		});
		return false;
	});
	
	$("#storeselect").bind("change", function(){
		var store = $(this).attr("value");
		window.location = '/shoppinglist/' + store;
	});
	
	$(".closepopup").click(function(){
		$(".nc_popup").hide();
		$("#coupBackgroundPopup").hide("fast");
	});
	
	$("#coupBackgroundPopup").click(function () {
		$(this).hide();
		$("#newpop").hide();
	});
	
	$(".cp_titles a").click(function (e) {
		var position = $(this).offset();
		$("#newpop").hide();
		var popup = $(this).siblings('.nc_popup');
		$("#newpop").html(popup.html());
		$("#newpop").css({"top":position.top - 135, "left":position.left + 105 });
		$("#coupBackgroundPopup").show();
		$("#newpop").show();
		$(".closepopup").click(function(){
			$("#newpop").hide();
			$("#newpop").html('');
			$("#coupBackgroundPopup").hide("fast");
		});
		return false;
	});
	
	$(".nc_popup .header a").click(function() {
		var loc = $(this).attr("href");
		window.open(loc)
	});
	
	$("#switchsale #saleid").bind("change", function(){
		var selected = $(this).attr("value");
		window.location = $(this).attr("value");
	});
	
	if ($("#printsl").length > 0) {
		$("#printsl").click(function() {
			window.print();
			return false;
		});
	}
	
	if ($("#sltable").length > 0) {
		 $("#sltable").tablesorter();
	};
	
	   $("#checkun").click(function(){
	        
	    });
	

	

		
});