var Site = {

	// this vars should be set in <head> server-side
	config: {
		base_url: '',
		site_url: ''
	},
	
	// this method is called on every page
	init: function() {

		// On Dom Ready
		jQuery(function($) {
			
			$('a[rel*=facebox]').facebox();
			$('.tooltip').qtip({
				position: {
					corner: {
						target: 'topMiddle',
						tooltip: 'bottomMiddle'
					}
				},
				style: {
	                 border: {
	                    width: 0,
	                    radius: 6,
						color: '#000'
	                 },
					width: {
						min: 120
					},
					fontSize: '1.1em',
					lineHeight: '1.5em',
					backgroundColor: '#000',
					fontFamily: 'Helvetica',
					padding: 10, 
					textAlign: 'center',
					tip: {
						corner: 'bottomMiddle', // We declare our corner within the object using the corner sub-option
						color: '#000',
						size: {
							x: 15,
							y : 8
						}
					},
					name: 'dark'
				},
				hide: 'mouseout'
			});
			$('.big_tooltip').qtip({
				position: {
					corner: {
						target: 'rightMiddle',
						tooltip: 'leftMiddle'
					}
				},
				style: {
	                 border: {
	                    width: 0,
	                    radius: 6,
						color: '#000'
	                 },
					fontSize: '1.1em',
					lineHeight: '1.5em',
					backgroundColor: '#000',
					fontFamily: 'Helvetica',
					padding: 10, 
					textAlign: 'left',
					tip: {
						corner: 'leftMiddle', // We declare our corner within the object using the corner sub-option
						color: '#000',
						size: {
							x: 8,
							y : 15
						}
					},
					name: 'dark'
				},
				hide: 'mouseout'
			});
			$('#show_hidden_tooltips').toggle(function(){ 
				$('.hidden_tooltips').show();
				$('#show_hidden_tooltips').html('Hide Tooltips');
				return false; 
			}, function(){
				$('.hidden_tooltips').hide();
				$('#show_hidden_tooltips').html('Show Tooltips');
				return false;
			});
			$('.zebra tr:even').addClass('alt'); // for zebra tables...
			$('.hint').input_hint();
			$('.displayer').bind('click', function() {
		        var $this = $(this);
				$($this.attr('href')).toggle();
				return false;
			});
			
			$('a.show').bind('click', function(e) {
			
				e.preventDefault();
				
				$this = $(this);
				
				if ($($this.attr('href')).css('display') !== 'none')
				{
					$($this.attr('href')).hide();
					$this.removeClass('show_active');
					return false;
				}
				
				rel = $this.attr('rel');
				$('a.show').each(function() {
					$this_inner = $(this);
					if ($this_inner.attr('rel') === rel)
					{
						$($this_inner.attr('href')).hide();
						$this_inner.removeClass('show_active');
					}
				});
				
				$($this.attr('href')).show();
				$this.addClass('show_active');
			
			});
			
			$('#checkall').toggle(function() {
				if ( $("#form").length > 0 ) {
					$('#form input[type=checkbox]').check_all();
				} else {
					$('input[type=checkbox]').check_all();
				}
				$('#checkall').attr('value', 'Uncheck All');
				return false; // So link doesn't execute
			}, function() {
				if ( $("#form").length > 0 ) {
					$('#form input[type=checkbox]').uncheck_all();
				} else {
					$('input[type=checkbox]').uncheck_all();
				}
				$('#checkall').attr('value', 'Check All');
				return false; // So link doesn't execute
			});
			
			$('#checkall_two').toggle(function() {
				$('#form_two input[type=checkbox]').check_all();
				$('#checkall_two').attr('value', 'Uncheck All');
				return false; // So link doesn't execute
			}, function() {
				$('#form_two input[type=checkbox]').uncheck_all();
				$('#checkall_two').attr('value', 'Check All');
				return false; // So link doesn't execute
			});
			
			$('select#' + $('#selectinator').attr('value')).show();
			
			$('#selectinator').bind('change', function() {
				$('#selectinator_sub select').hide();
		        var $this = $(this);
				$('select#' + $this.attr('value')).show();
				window.location.replace($('select#' + $this.attr('value')).attr('value'));
			});
			
			$('#selectinator_sub select').bind('change', function() {	
	        	var $this = $(this);
				window.location.replace($this.attr('value'));
			});
			
			
			$('#selectinator_domain').bind('change', function() {	
	        	var $this = $(this);
				window.location.replace($this.attr('value'));
			});
			
			$('#selectinator_inner select').bind('change', function() {
	        	var $this = $(this);
				window.location.replace($this.attr('value'));
			});
			
			$('a#run_report').bind('click', function() { 
				$this = $(this);
				$('#non_existant').load($this.attr('class'));
			});
			
		});
		
		// On Window Load
		jQuery(window).load(function ($) {

		});
		
		// Load Immediately
		(function($) {
		
		})(jQuery);

		if ($.browser.msie && $.browser.version <= 6 )
		{
			// gay stuff goes here
		}
	},
	
	// this method is called on every page
	settings: function() {

		// On Dom Ready
		jQuery(function($) {
			
			$('.hover_link').bind('mouseover', function() {
				$this = $(this);
				$('#' + $this.attr('id') + '_child').show();
			}).bind('mouseout', function() {
				$this = $(this);
				$('#' + $this.attr('id') + '_child').hide();
			});
			
			$('ul.table_tabs li a').bind('click', function(e) {
				
				e.preventDefault();

				$this_link = $(this);

				// If it's already being shown lets hide it and stop
				if ($($this_link.attr('href')).css('display') != 'none')
				{
					$($this_link.attr('href')).hide();
					$this_link.removeClass('table_tab_active');
				}
				else
				{
					// Lets hide all items that are related before we show the current one
					$('a[rel*=' + $this_link.attr('rel') + ']').each(function() {
						$this_links = $(this);
						$($this_links.attr('href')).hide();
						$this_links.removeClass('table_tab_active');
					});
				
					$($this_link.attr('href')).show();
					$this_link.addClass('table_tab_active');
				}
			
			});
			
		});
			
	},
	
	// this method is called on every page
	username_checker: function() {

		// On Dom Ready
		jQuery(function($) {
			$('#signup_username').bind('keyup', function() {
		        var $this = $(this);
				//$("#availability").html($this.val);
				$("#availability").load('/usercheck', { 'username': $this.val() } );
			});
		});
			
	},
	
	signup: function(json) {
		
		// On Dom Ready
		jQuery(function($) {
			var obj = jQuery.parseJSON(json);
			Site.update_summary(obj);
			$('input[name="signup\\[sjproduct\\]"], #signup_whitelabel, #signup_weekly_reports').bind('change', function() {
				Site.update_summary(obj);
			});
		});
		
	},
	
	update_summary: function (obj)
	{
		$.each(obj, function(i,item){
			if (i === $('input[name="signup\\[sjproduct\\]"]:checked').val())
			{
				if (i === 'Quicktry')
				{
					$('#signup_weekly_reports').removeAttr('checked');
					$('#signup_whitelabel').removeAttr('checked');
					$('#package_options_container').hide();
				}
				else
				{
					$('#package_options_container').show();
				}
				$('#package_details_wrapper').show();
				$('#package_nothing').hide();
				$('#package_info').show();
				$('#package_name').html(item.name + ':');
				if (item.isFree == 1)
				{
					$('#package_price').html('FREE');
					var price = '0.00';
				}
				else
				{
					$('#package_price').html('$' + item.recurringChargeAmount);
					var price = item.recurringChargeAmount;
				}
				
				price = parseFloat(price);
				
				if ($('#signup_whitelabel').attr('checked') === true)
				{
					price = price + parseFloat(item.items.WL.overageAmount);
					$('#report_options_wrapper').show();
					$('#whitelabel_row').show();
					$('#whitelabel_price').html(item.items.WL.overageAmount + '.00');
				}
				
				if ($('#signup_weekly_reports').attr('checked') === true)
				{
					price = price + parseFloat(item.items.WR.overageAmount);
					$('#report_options_wrapper').show();
					$('#weekly_reports_row').show();
					$('#weekly_reports_price').html(item.items.WR.overageAmount + '.00');
				}
				
				if ($('#signup_weekly_reports').attr('checked') !== true && $('#signup_whitelabel').attr('checked') !== true)
				{
					$('#report_options_wrapper').hide();
				}
				
				if ($('#signup_weekly_reports').attr('checked') !== true)
				{
					$('#weekly_reports_row').hide();
				}
				
				if ($('#signup_whitelabel').attr('checked') !== true)
				{
					$('#whitelabel_row').hide();
				}

				$('#monthly_total').html('$' + price + '.00');
				
				var comp_num = item.items.Competitors.quantityIncluded;
				
				if (comp_num == 9999)
				{
					comp_num = 'unlimited';
				}
				else
				{
					comp_num = Site.add_commas(comp_num);
				}
				
				$('#package_info').html('<div class="clearfix"><strong>Competitors:</strong> <span>' + comp_num + '</span></div><div class="clearfix"><strong>Keywords:</strong> <span>' + Site.add_commas(item.items.Keywords.quantityIncluded) + '</span></div><div class="clearfix"><strong>Pages:</strong> <span>' + Site.add_commas(item.items.Page.quantityIncluded) + '</span></div><div class="clearfix"><strong>Automated Reports:</strong> <span>1 / mo</span></div>');
			}
          });
	},
	
	add_commas: function (nStr)
	{
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	},
	
	// this method is called on every page
	smx_promo: function() {

		// On Dom Ready
		jQuery(function($) {
			$('.button_grey_na').bind('click', function() {
		        $('p').hide();
			});
		});
			
	},
	
	// this method is called on every page
	retail: function() {

		// On Dom Ready
		jQuery(function($) {
			
			$('#juicy_tooltip').qtip(
			   {
			      content: $('#juicy_tooltip_content').html(),
					position: {
						corner: {
							target: 'topMiddle',
							tooltip: 'bottomMiddle'
						}
					},
					style: {
		                 border: {
		                    width: 0,
		                    radius: 6
		                 },
						width: {
							min: 120
						},
						fontSize: '1.1em',
						lineHeight: '1.5em',
						fontFamily: 'Helvetica',
						padding: 10, 
						textAlign: 'left',
						tip: {
							corner: 'bottomMiddle', // We declare our corner within the object using the corner sub-option
							size: {
								x: 15,
								y : 8
							}
						},
						name: 'cream'
					},
					hide: 'mouseout'
			   });
			
			var headline_count;
			var headline_interval;
			var old_headline = 0;
			var current_headline = 0;

			$(document).ready(function(){
			  headline_count = $("div.headline").size();
			  $("div.headline:eq("+current_headline+")").css('top','5px');

			  headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
			  $('#scrollup').hover(function() {
			    clearInterval(headline_interval);
			  }, function() {
			    headline_interval = setInterval(headline_rotate,5000); //time in milliseconds
			    headline_rotate();
			  });
			});

			function headline_rotate() {
			  current_headline = (old_headline + 1) % headline_count; 
			  $("div.headline:eq(" + old_headline + ")").animate({top: -205},"slow", function() {
			    $(this).css('top','210px');
			  });
			  $("div.headline:eq(" + current_headline + ")").show().animate({top: 5},"slow");  
			  old_headline = current_headline;
			}
						
		});
		
	},
	
	// this method is called on every page
	pricing: function() {

		// On Dom Ready
		jQuery(function($) {
			$('#pricing_more_details').toggle(function() {
		        var $this = $(this);
				$this.html('Show Less Details <img src="/images/retail/up_arrow.png" width="22" height="10" alt="Up Arrow" />');
				$('#pricing_more_details_container').slideDown();
				return false;
			}, function() {
		        var $this = $(this);
				$this.html('Show More Details <img src="/images/retail/down_arrow.png" width="22" height="10" alt="Down Arrow" />');
				$("#pricing_more_details_container").slideUp();
				return false;
			});
		});
			
	},
	
	// this is for the sb widget
	sb_widget_box: function() {

		// On Dom Ready
		jQuery(function($) {
			
			var options = { 
			        target: '#the_widget_callback_modal',   // target element(s) to be updated with server response 
					beforeSubmit: showResponse
			    }; 

		    $('#the_widget_form').ajaxForm(options);
		
		});
		
	},
	
	analyzer: function() {
		
		jQuery(function($) {
			
			var options = { 
		        target:        '#left_column',   // target element(s) to be updated with server response 
				type: 		   'post'
		    }; 

		    // bind to the form's submit event 
		    $('#analyzer input.submit').bind('click', function() { 
			
		        $('#analyzer').ajaxSubmit(options); 
		
		        return false; 
		    });
			
		});
		
	},
	
	scanner: function() {
		
		jQuery(function($) {
			
			var options = { 
		        target:        '#left_column',   // target element(s) to be updated with server response 
				type: 		   'post'
		    }; 

		    // bind to the form's submit event 
		    $('#scanner input.submit').bind('click', function(e) { 
			
		    	e.preventDefault();
		    	
		    	$('#loader').show();
		    	//$('#scanner').ajaxSubmit(options); 
		    	$('#loader').hide();
		
		        return false; 
		    });
			
		});
		
	},
	
	getting_started: function() {
		
		jQuery(function($) {
			
			$('a.show_archive').bind('click', function(e) {
			
				e.preventDefault();
				
				$this = $(this);
				
				if ($($this.attr('href')).css('display') !== 'none')
				{
					$this.html('Show archived pages for this domain.')
					$($this.attr('href')).hide();
					$this.removeClass('show_active_archive');
					return false;
				}
				
				rel = $this.attr('rel');
				$('a.show').each(function() {
					$this_inner = $(this);
					if ($this_inner.attr('rel') === rel)
					{
						$($this_inner.attr('href')).hide();
						$this_inner.removeClass('show_active_archive');
					}
				});
				
				$this.html('Hide archived pages for this domain.')
				$($this.attr('href')).show();
				$this.addClass('show_active_archive');
			
			});
			
			$('#run_multiple_domains').bind('click', function() {
					var options = { 
				        target:        '#non_existant',   // target element(s) to be updated with server response 
						type: 		   'get',
						url:       	   $('#run_multiple_domains').attr('title')
				    };
			    $('#domain_form').ajaxSubmit(options);
				
				jQuery.facebox('<div class="default_modal"><h2>Your Report</h2><p>You\'re report is being run, you will recieve an email when it is ready for you.</p><p class="clearfix"><div class="float_left"><a href="#" onclick="jQuery(document).trigger(\'close.facebox\'); return false;" class="button_grey_na">Close</a></div></p></div>');
			});
			
			$('.domain_label').bind('mouseover', function() {
				$this = $(this);
				$('#' + $this.attr('id') + '_child').show();
			}).bind('mouseout', function() {
				$this = $(this);
				$('#' + $this.attr('id') + '_child').hide();
			});
			
		});
		
	},
	
	big_paginator: function() {
	
		jQuery(function($) {
					
			$("table .entry_row").quickpaginate({ perpage: 10, showcounter: true, pager : $("#main_paginator") });	
		});
		
	},
	
	paginator: function() {
	
		jQuery(function($) {
					
			$("table .row").quickpaginate({ perpage: 6, showcounter: true, pager : $("#paginator") });	
		});
		
	},
	
	notice: function() {
	
		jQuery(function($) {
			$('#hider').bind('click', function(e){
				e.preventDefault();
				$this = $(this);
				$('#hider_container').slideUp();
				$this.load($this.attr('href')).hide();
			});
		});
		
	},
	
	cityvoice: function() {
		
		// On Dom Ready
		jQuery(function($) {
			
			var options = { 
			        target: '#results_container',   // target element(s) to be updated with server response 
					beforeSubmit: function () {
						$('#results_container').html('<img src="/images/ajax-loader.gif" />');					}
			    }; 

		    $('#cityvoice_analyze').ajaxForm(options);	
		});
	},
	
	// this is for the orders page
	orders: function() {

		// On Dom Ready
		jQuery(function($) {
			
			$('.hint').input_hint();
			
			$('.select_pages').bind('change', function() {
				$this = $(this);
				var i=1;
				var loop_limit = $this.attr('title');
				while (i < loop_limit)
				{
					if(i <= $this.attr('value'))
						$('#' + $this.attr('id') + i).show();
					else
						$('#' + $this.attr('id') + i).hide();
					i++;
				}
			});
			
			$('.help_popup').bind('mouseenter', function() {
				$this = $(this);
				$($this.attr('href')).fadeIn();
			}).bind('mouseleave', function() {
				$this = $(this);
				$($this.attr('href')).fadeOut();
			});
			
			$('.hint_popup').bind('mouseenter', function() {
				$this = $(this);
				$($this.attr('href')).fadeIn();
			}).bind('mouseleave', function() {
				$this = $(this);
				$($this.attr('href')).fadeOut();
			});
			$('.left_dropdown').bind('click', function() {
				$this = $(this);
				$($this.attr('href')).toggle();
				return false
			});
			
			$('.hint').input_hint();
			
			$('.unselect').bind('click', function() {
				$this = $(this);
				$('#' + $this.attr('title')).fattr('checked',false);
				$('#' + $this.attr('title') + '_install').attr('checked',false);
				return false;
			});

			$("#platforms li a").click(function() {
				$("#site_cms").val($(this).attr("rel"));
			});
			var options = { 
		        target:        '.total',   // target element(s) to be updated with server response 
				type: 		   'post',
				url:       	   '/fixmysite/index.php/orders/total'         // override for form's 'action' attribute 
				
		        // other available options: 
		        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
		        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
		        //clearForm: true        // clear all form fields after successful submit 
		        //resetForm: true        // reset the form after successful submit 

		        // $.ajax options can be used here too, for example: 
		        //timeout:   3000 
		    };
			var price_options = { 
		        target:        '.total_price',   // target element(s) to be updated with server response 
				type: 		   'post',
				url:       	   '/fixmysite/index.php/orders/pricetotal' 		    
		    };
		/*
			var options_url = {
			target:	      '#domain_url',
				type:		'post',
				url:		'/fixmysite/index.php/orders/step-two'
		    };
		*/
		$("#platforms").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			visible: 6
		});
			$("#page_count").slider({
				range: "min",
				value: 1,
				min: 1,
				max: 15,
				slide: function(event, ui) {
					$("#site_num_pages").val(ui.value);
					$('#order_form_wrapper').ajaxSubmit(options); 
		        		$('#order_form_wrapper').ajaxSubmit(price_options); 
				}
			});
			$("#site_num_pages").attr("readonly", "readonly");
			$("#site_num_pages").val($("#page_count").slider("value"));

			$('#site_domain').bind("change", function() {
				var value = $(this).val();
			 	$('#fixmysite_orgurl').val(value);
			});
	        $('#order_form_wrapper').ajaxSubmit(options);
	        $('#order_form_wrapper').ajaxSubmit(price_options); 

		    // bind to the form's submit event 
		    $('#order_form_wrapper input').bind('change', function() { 
		        $('#order_form_wrapper').ajaxSubmit(options); 
		        $('#order_form_wrapper').ajaxSubmit(price_options); 
		
		        return false; 
		    });

			    // bind to the form's submit event 
			    $('#order_form_wrapper select').bind('change', function() { 

			        $('#order_form_wrapper').ajaxSubmit(options); 

			        return false; 
			    });
		
		    // bind to the form's submit event 
		    $('.unselect').bind('click', function() { 
			
		        $('#order_form_wrapper').ajaxSubmit(options); 
		
		        return false; 
		    });
		
			$('#step_one_button').bind('click', function() {
				$('#step_two').show();
				$('#step_one').hide();
				return false;
			});
			$('#step_two_button').bind('click', function() {
				$('#step_two').hide();
				$('#step_one').show();
				return false;
			});
		
		});
		
	}
	
};


Site.init();