$(document).ready(function(){
	PAGE_TABS.init();
});
var PAGE_TABS = {
	tab_width : "",
	timeout : 250,
	closetimer : 0,
	rac_opened : false,
	question_opened : false,
	validation_options:{
		errorPlacement : function(o_error, dom_el){
			return true;
		},
		highlight: function(element, errorClass, validClass) {
			$('#' + $(element).attr('id') + '_error').addClass('cross');
		},
		unhighlight: function(element, errorClass, validClass) {
			$('#' + $(element).attr('id') + '_error').removeClass('cross');
		},
		invalidHandler: function(form, validator) {
			$('#' + $(this).attr('id') + '_error').fadeIn(400).delay(3000).fadeOut(400);
		}
	},
	init : function(){
		if ($('#current_category img').attr("alt") == "Australia"){
			PAGE_TABS.tab_width = "148px";
		}else if ($('#current_category img').attr("alt") == "United Kingdom"){
			PAGE_TABS.tab_width = "189px";
		}else if ($('#current_category img').attr("alt") == "United States"){
			PAGE_TABS.tab_width = "174px";
		}else if ($('#current_category img').attr("alt") == "New Zealand"){
			PAGE_TABS.tab_width = "173px";
		}else{
			PAGE_TABS.tab_width = "200px";
		}
		//PAGE_TABS.tab_width = $('#changecategory').width()
		$('#changecategory').click(function(){
			$('#changecategory').animate({width: '200px'}, 300, 'linear', function(){
				$(this).animate({height: '135px'}, 300, 'linear', function(){
					$('#category_change_container').show();
				});
			});
		});

		$('#changecategory').bind('mouseout', function(){PAGE_TABS.dd_timer()});
		$('#changecategory').bind('mouseover', PAGE_TABS.dd_canceltimer);

		$('.category_outer').click(function() {
			window.location = $(this).attr("app_path") + "change_region.php?id=" + $(this).attr("id") + "&dest=" + $(this).attr("dest_domain");
		});

		$('#twitter').click(function(){
			window.open("http://twitter.com/realsatisfied","rstwitter");
		});

		$('#facebook').click(function(){
			window.open("http://facebook.com/realsatisfied","rsfacebook");
		});

		$('#havequestion').click(function(){
			if(!PAGE_TABS.question_opened){
				PAGE_TABS.close_open_tabs();
				PAGE_TABS.question_opened = true;
				$('#havequestion').animate({width: '440px'}, 300, 'linear', function(){
					$(this).animate({height: '420px'}, 300, 'linear', function(){
						$('#question_container').show();
						if ($('#question_fullname').val() == ""){
							$('#question_fullname').val(PAGE_TABS.readCookie('my_name'));
						}
						if ($('#question_sEmail').val() == ""){
							$('#question_sEmail').val(PAGE_TABS.readCookie('email'));
						}
					});
				});
			}
		});

		$('#question_button').click(function(event){
			if ($('#question_button').html() == "Ask the Team"){
				$('#question_button').html('Please Wait...');
				if ($('#question_form').valid()){
					$.post(	"main.php?c=exclusion&f=help_request",
						{
							fullname: $('#question_fullname').val(),
							sEmail: $('#question_sEmail').val(),
							sPhone: $('#question_sPhone').val(),
							tNote: $('#question_tNote').val(),
							xCategory: $('#question_xCategory').val(),
							category_desc: $('#question_category_desc').val()
						},
						function(data) {
							if(data == 0){
								$('#question_inner').html("<div class=\"thanks\"><p class=\"thanks_heading\">Thanks for your question</p><p>We'll be in touch shortly.</p></div>");
							}else{
								$('#question_inner').html("<div class=\"thanks\"><p class=\"thanks_heading\">Oooops</p><p>Sorry, we seem to be having problems processing your request. Please try again later.</p></div>");
							}
						}
					);
				}else{
					$('#question_button').html("Ask the Team");
				}
			}
			event.stopPropagation();
		});

		$('#question_closebutton').click(function(event){
			event.stopPropagation();
			PAGE_TABS.close_question_tab();
		});
		
		$('#question_form').validate(PAGE_TABS.validation_options);

		$('#requestacall').click(function(){
			if(!PAGE_TABS.rac_opened){
				PAGE_TABS.close_open_tabs();
				PAGE_TABS.rac_opened = true;
				$('#requestacall').animate({width: '440px'}, 300, 'linear', function(){
					$(this).animate({height: '420px'}, 300, 'linear', function(){
						$('#rac_container').show();
					});
				});
			}
		});

		$('#rac_button').click(function(event){
			if ($('#rac_button').html() == "Request a Call"){
				$('#rac_button').html('Please Wait...');
				$('#rac_note_layer').hide();
				if ($('#rac_form').valid()){
					$.post(	"main.php?c=exclusion&f=help_request",
						{
							fullname: $('#rac_fullname').val(),
							sEmail: $('#rac_sEmail').val(),
							sPhone: $('#rac_sPhone').val(),
							tNote: $('#rac_tNote').val(),
							xCategory: $('#rac_xCategory').val(),
							category_desc: $('#rac_category_desc').val()
						},
						function(data) {
							if(data == 0){
								$('#rac_inner').html("<div class=\"thanks\"><p class=\"thanks_heading\">Thanks for your request</p><p>We do our best to call you back on the same day, Monday to Friday. Thanks for your patience, we'll be in touch soon</p></div>");
							}else{
								$('#rac_inner').html("<div class=\"thanks\"><p class=\"thanks_heading\">Oooops</p><p>Sorry, we seem to be having problems processing your request. Please try again later.</p></div>");
							}
						}
					);
				}else{
					$('#rac_button').html("Request a Call");
				}
			}
			event.stopPropagation();
		});

		$('#rac_closebutton').click(function(event){
			event.stopPropagation();
			PAGE_TABS.close_rac_tab();
		});

		$('#rac_note_layer').click(function(){
			$('#rac_note_layer').hide();
			$('#rac_tNote').focus();
		});

		$('#rac_tNote').focus(function(){
			$('#rac_note_layer').hide();
		});

		$('#rac_form').validate(PAGE_TABS.validation_options);
	},
	close_category_tab: function(){
		$('#category_change_container').slideUp();
		$('#changecategory').animate({height: '16px'}, 300, 'linear', function(){
			$(this).animate({width: PAGE_TABS.tab_width}, 300);
		});
	},
	dd_timer: function(){
		PAGE_TABS.closetimer = window.setTimeout(PAGE_TABS.close_category_tab, PAGE_TABS.timeout);
	},
	dd_canceltimer: function(){
		if(PAGE_TABS.closetimer){
			window.clearTimeout(PAGE_TABS.closetimer);
			PAGE_TABS.closetimer = null;
		}
	},
	close_question_tab: function(){
		PAGE_TABS.question_opened = false;
		$('#question_container').hide();
		$('#havequestion').animate({height: '16px'}, 300, 'linear', function(){
			$(this).animate({width: '90px'}, 300);
		});
	},
	close_rac_tab: function(){
		PAGE_TABS.rac_opened = false;
		$('#rac_container').hide();
		$('#requestacall').animate({height: '16px'}, 300, 'linear', function(){
			$(this).animate({width: '110px'}, 300);
		});
	},
	close_open_tabs: function(){
		if (PAGE_TABS.rac_opened){
			PAGE_TABS.close_rac_tab();
			PAGE_TABS.pause(600);
		}
		if (PAGE_TABS.question_opened){
			PAGE_TABS.close_question_tab();
			PAGE_TABS.pause(600);
		}
	},
	readCookie: function(c_name) {
		if (document.cookie.length>0){
			var c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1){
				c_start=c_start + c_name.length+1;
				var c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end).replace(/\+/g,'%20'));
			}
		}
		return "";
	},
	pause: function(milliseconds) {
		var dt = new Date();
		while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
	}
}


