var errors = { '404': 'We can\'t find content source file please try again later.', '500': 'Server problems. Please try again later..' };

function close_fb() {
  $.fancybox.close();
  //set cookie for 10 days
  $.cookie("popup_offer", "2", { expires: 10, path: '/' });
}

function add_focus(id) {
$("#"+id).addClass("focus");
}

function remove_focus(id) {
$("#"+id).removeClass("focus");
}

function reset_form(div) {
$(":input","#"+div).not(":button, :submit, :reset").val("").removeAttr("checked").removeAttr("selected");
$("#"+div).find("option:first").attr("selected", "selected").parent("select");
}

$().ready(function() {

//fire cookie
if($.cookie("popup_offer") === null) {
$.cookie("popup_offer", "1", { path: '/' });
}

$("#ppo_form").bind("submit", function() {

ref = $(document)[0].referrer;
loc = $(location).attr('href');

send_data = $(this).serialize();
send_data += "&ref=" + encodeURIComponent(ref);
send_data += "&loc=" + encodeURIComponent(loc);

$(".ppo_error").empty();

	$.fancybox.showActivity();
	$.ajax({
		type: "POST",
		url: "http://"+document.domain+"/ppo/process.php",
		data: send_data,
		dataType: 'json',
		async: false,
		cache: false,
		success: function(result){
        $.fancybox.hideActivity();
        if(result.type == 'success')
        {
		$(".ppo_buttons, .ppo_error").hide();
		$(".ppo_success").html(result.content).fadeIn("slow");
		$.cookie("popup_offer", "2", { expires: 10, path: '/' });
        }
		
		if(result.type == 'error')
        {
        $(".ppo_error").html(result.errors).fadeIn("slow");
        }

},
error:function(xhr,err){
$.each(errors, function(err, msg) {
if (xhr.status == err) { 				
alert(msg);
$.fancybox.close();
}
});
}
});

return false;
});
  
ppo_start = function() {

  //check if we have pop up cookie
  if($.cookie("popup_offer") == "1") {
  
  //show pop-up
  $(".ppo_trigger").fancybox({
	'scrolling'	: 'no',
	'opacity'   : true,
	'modal'     : true,
	'titleShow'	: false,
	'onStart'	: function() {
	reset_form("ppo_form");
	},
	'onClosed'	: function() {
	$(".ppo_error").hide();
	}
  }).trigger('click');
  }
  //$.cookie("popup_offer",null);
 } 

$(window).load(ppo_start);
});
