var click_array = {}, exp = new Date(),
	overlay, banners_overlay, ajax_overlay, account_overlay;
exp.setTime(exp.getTime() + (12*60*60*1000));

function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ((secure) ? "; secure" : "");
}

function record_click(click_id, url) {
	var clicks = $("#clicks");
	var click_count = clicks.html();
	window.open(url);
//	if (!click_array[click_id] && click_count > 0) {
	if (click_count > 0) {
		if (click_count == 1) {
			overlay.dialog('open');
		} else {
			click_array[click_id] = 1;
			clicks.html( click_count - 1 );
		}
	}
}

function show_account() {
	account_overlay.dialog("option", "title", "Account options");
	account_overlay.dialog("open");	
	return void(0);
}

function get_ad_id(calling){
	return $(calling).parents("tr.editable_ad").attr("id").split("editable_ad_").join("");
}

function repeatable_highlights(){
	$("input:submit").button();
}

$(function() {

	$(".edit_ad_form").live("submit", function(){
		var uln = $(this).find('[name=n]').val();
		$.ajax({
			url: 'edit',
			type: 'POST',
			data: $(this).serialize(),
			success: function(msg){
				ajax_overlay.dialog("close");
				if (uln==1)
					$("#edit_site_holder").html(msg);
				else
					$("#edit_ads_holder").html(msg);
			}		
		});
		return false;
	});

	var acc_handle = function () {
		var id = $(this).find('[name=id]').val();
		$.ajax({
			url: 'account',
			type: 'POST',
			data: $(this).serialize(),
			success: function(msg){
				$("#account_answer_"+id).show()
					.html(msg).delay(2000).slideToggle('slow');
				/* if (msg.match('green'))
					$(this).find('[type=text]').val(''); */
			}
		});
		return false;
	}

	$(".account_form").live("submit", acc_handle);

	$("select.ad_status_change").live("change", function(){
		$.ajax({
			url: 'edit?n=' + get_ad_id(this) + '&amp;to=' + $(this).val(),
			global: false,
			success: function(msg) {},
			complete: function(msg) {}
		});
	});

	$("img.delete_button").live("click", function(){
		var holder = $(this).parents("tr.editable_ad");
		$.ajax({
			url: 'edit?n=' + get_ad_id(this) + "&amp;to=d",
			global: false,
			success: function(msg) {
				holder.fadeOut(function(){
					holder.remove();
				});
			}
		});
	});
	
	$("img.edit_button, span.edit_button").live("click", function(){
		$.ajax({
			url: 'edit?n=' + get_ad_id(this),
			global: false,
			success: function(msg){
				$("#ajax_overlay").html(msg);
				ajax_overlay.dialog("option", "title", "Edit your ad");
				ajax_overlay.dialog("open");	
				repeatable_highlights();
			}
		});
	});

	$("body").mouseenter(function () {
		if (document.track_code) {
			$.ajax({
				url: 'track?c=' + document.track_code,
				global: false,
				success: function (msg) { /* alert('tracked ' + document.track_code) */ }
			});
		}
		document.track_code = null;
	});
	
	$(".draggable").draggable();

	$("#top_site_holder").show("blind", {}, "slow");
	$("#promo_holder").show("blind", {}, "slow");

	$("td.top_site").mouseenter(function() {
		$(this).removeClass("top_site_unfocus").addClass("top_site_focus");
	});
	$("td.top_site").mouseleave(function() {
		$(this).removeClass("top_site_focus").addClass("top_site_unfocus");
	});

	$("td.promo").mouseenter(function() {
		$(this).addClass("promo_focus");
	});
	$("td.promo").mouseleave(function() {
		$(this).removeClass("promo_focus");		
	});
	
	$("tr.stripable").mouseenter(function() {
		$(this).css("background-color", "#e8ebfd");
	});
	$("tr.stripable").mouseleave(function() {
		$(this).css("background-color", "");
	});

	var top_nav_internal = $("#top_nav_internal");
	$("div.nav_link", top_nav_internal).mouseenter(function() {
		$(this).css("background-color", "#3471ff");
	});	
	$("div.nav_link", top_nav_internal).mouseleave(function() {
		$(this).css("background-color", "");
	});

	$("#credit_options").change(function() {
		$.ajax({
			url: 'credit_box?time_choice=' + $('#credit_options').val(),
			global: false,
			beforeSend: function() {
				$('.credit_box_col').fadeOut();
			},
			success: function(msg) {
				$('#credit_box').html(msg);
				$('.credit_box_col').hide();
				$('.credit_box_col').fadeIn();
			}
		});
	});

	ajax_overlay = $("#ajax_overlay").dialog({
		autoOpen: false, modal: true, title: '', width: 800, 
		draggable: true, resizable: true, closeOnEscape: true, hide: 'slide', show: 'slide',
		open: function(event, ui) { $(".ui-dialog-titlebar").show(); }
	});	

	overlay = $("#overlay").dialog({
		autoOpen: false, modal: true, title: 'Add your site right now!', width: 800, 
		draggable: true, resizable: true, closeOnEscape: true, hide: 'slide', show: 'slide',
		open: function(event, ui) { $(".ui-dialog-titlebar").show(); }
	});	

	banners_overlay = $("#banners_overlay").dialog({
		autoOpen: false, modal: true, title: 'Sample promotions...', width: 800,
		draggable: true, resizable: true, closeOnEscape: true, hide: 'slide', show: 'slide',
		open: function(event, ui) { $(".ui-dialog-titlebar").show(); }
	});	

	account_overlay = $("#account_overlay").dialog({
		autoOpen: false, modal: true, title: 'Account settings', width: 800,
		draggable: true, resizable: true, closeOnEscape: true, hide: 'slide', show: 'slide',
		open: function(event, ui) { $(".ui-dialog-titlebar").show(); }
	});	

	repeatable_highlights();

});


