/**
 * website.js
 */

var website = {
	
	init: function() {
		
		website.deadenlinks();
		website.init_homepage_search();
		website.header_search_init();
		website.init_sticky_ads();
		website.check_if_mobile_device();
		website.local_information_init();
		website.tabs_init();
		//website.init_social_icons();
		website.init_listings_tools();
		website.init_external_links();

		website.init_editlisting_form();
		
		if( $.browser.mobile ) {
			$('.desktop-container').hide();
			$('.mobile-container').show();
		}

		//$(".lightboxoutside").colorbox({width:"80%", height:"80%", iframe:true, href: 'http://www.google.co.uk'});
		
		$('.listing').hover(function() {
			$(this).find('.edit-business-link').show();
		}, function() {
			$(this).find('.edit-business-link').hide();
		});

		$(".tweet").tweet({
			join_text: "auto",
			username: "rightcareindex",
			//avatar_size: 16,
			count: 5,
			auto_join_text_default: "we said:<br />",
			auto_join_text_ed: "we",
			auto_join_text_ing: "we were",
			auto_join_text_reply: "we replied",
			auto_join_text_url: "we were checking out",
			loading_text: "loading tweets..."
		});

	}
	
	,init_editlisting_form: function() {
		
		$('.fieldset-upgrades input').click(function() {
			switch( $(this).val() ) {
				case 'Featured':
					$('.payment-icons').slideDown('fast');
				break;
				case 'Highlighted':
					$('.payment-icons').slideDown('fast');
				break;
				case '':
					$('.payment-icons').slideUp('fast');
				break;
			}
		});

		$("input[name='upgradeoption']").click(function() {
			var value = $(this).val();
			if( value == 'Featured' ) {
				$('.featured-info-container').slideDown('fast');
				$('#featured_content').focus();
			} else {
				$('.featured-info-container').slideUp('fast');
			}
		});
		
		$('#addeditlistingform').validationEngine();

	}

	,deadenlinks: function() {

		$('a').click(function() {
			
			if( $(this).attr('href') == '#' ) {
				return false;
			}
			
		});

	}
	
	,mobile_enable_site: function() {
	}
	
	,init_external_links: function() {

		$("a[href^='http']").live('click', function() {
			if( !$(this).hasClass('lightboxoutside') ) {
				var hostname = window.location.hostname;
				var external = $(this).attr('href');
				if( url.match( hostname ) ) {
					return;
				}
				window.open( external );
				return false;
			}
		})

	}
	
	,init_social_icons: function() {

		$('.social-icons a').hover(function() {
											
			$(this).find('img').stop().animate({
				marginTop: 0
			}, 'fast');
			
		}, function() {

			$(this).find('img').stop().animate({
				marginTop: '16px'
			}, 'fast', 'easeOutBounce');

		}).click(function(e) {
			//e.preventDefault();
			//$(this).blur();
			//window.open( $(this).attr('href') );
		});

	}
	
	,init_homepage_search: function() {

		$(".quicksearchform select[name='county']").change(function() {
			$(".quicksearchform input[name='keywords']").focus();
		});

		$(".quicksearchform input[name='keywords']").focus(function() {
			if( $(this).val() == $(this).attr('title') ) {
				$(this).removeClass('ghosted').val('');
			}
		}).blur(function() {
			if( $(this).val() == '' ) {
				$(this).addClass('ghosted').val( $(this).attr('title') );
			}
		}).trigger('blur');
		
		$('.quicksearchform').submit(function(e) {
			e.preventDefault();
			
			var keywords = $(this).find('#keywords').val();
			var county = $(this).find('#county').val();

			if( keywords != 'enter keywords (name, address, etc)' && keywords != '' ) {
				var url  = '/find/' + escape(keywords).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '').replace('@', '%40');
				
				if( county ) {
				    url += '/in/' + escape(county).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '').replace('@', '%40');
				}
				
				url += '.html';
					
				window.location = url;
			} else if(county && keywords == 'enter keywords (name, address, etc)') {
			    var url = '/find/in/' + escape(county).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '').replace('@', '%40');
				
				url += '.html';
					
				window.location = url;
			}

			
			return false;
		});

	}
	
	,init_sticky_ads: function() {
		
		var $sidebar   = $(".side-ads-zone"),
			$window    = $(window),
			offset     = $sidebar.offset(),
			topPadding = 15;
	

		$window.scroll(function() {
			if ($window.scrollTop() > offset.top) {
				$sidebar.stop().animate({
					marginTop: $window.scrollTop() - offset.top + topPadding
				});
			} else {
				$sidebar.stop().animate({
					marginTop: 0
				});
			}
		});



		/*
		var top = offset.top - parseFloat( $sidebar.css('margin-top').replace(/auto/, 0) );
		$window.scroll(function () {
			// what the y position of the scroll is
			var y = $(this).scrollTop();
      
			// whether that's below the form
			if (y >= top) {
				// if so, ad the fixed class
				$sidebar.addClass('fixed');
			} else {
				// otherwise remove it
				$sidebar.removeClass('fixed');
			}
		});
		*/

	}
	
	,local_information_init: function() {

		$('.about_a_county').change(function() {
			var slug = $(this).val();
			if(slug) {
				window.location = '/your/' + slug + '.html';
			}
		});

		$('.nhs_in').change(function() {
			var slug = $(this).val();
			if(slug) {
				window.location = '/nhs/' + slug + '.html';
			}
		});

	}
	
	,header_search_init: function() {

		$("#header_search_form input[name='keywords']").focus(function() {
			if( $(this).val() == $(this).attr('title') ) {
				$(this).removeClass('ghosted').val('');
			}
		}).blur(function() {
			if( $(this).val() == '' ) {
				$(this).addClass('ghosted').val( $(this).attr('title') );
			}
		}).trigger('blur');

		$('#header_county').change(function() {
			if( $(this).val() != '' ) {
				$(this).next().focus();
			}
			/*
			var category = $('#header_category').val();
			var county = $(this).val();
			if(category && county) {
				var url = '/browse/' + category + '/in/' + county + '.html';
				window.location = url;
			} else if(category && !county) {
				var url = '/browse/' + category + '.html';
				window.location = url;
			}
			*/
		});
		
		$('#header_search_form').submit(function(e) {
			e.preventDefault();
			
			var county = $('#header_county').val();
			var keywords = $('#header_county').next().val();

			if(county && keywords != 'enter keywords here' && keywords != '') {
				var url  = '/find/' + escape(keywords).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '').replace('@', '%40');
				
				if( county ) {
				    url += '/in/' + escape(county).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '').replace('@', '%40');
				}
				
				url += '.html';
					
				window.location = url;
			} else if(county && keywords == 'enter keywords here') {
			    var url = '/find/in/' + escape(county).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '').replace('@', '%40');
				
				url += '.html';
					
				window.location = url;
			} else if(keywords != 'enter keywords here' && keywords != '') {
				var url = '/find/' + escape(keywords).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '').replace('@', '%40');
				url += '.html';
				
				window.location = url;
			}
			
		});

	}
	
	,tabs_init: function() {
		
		$('.tabs a').click(function(e) {
			if( !$(this).hasClass('selected') ) {
				var rel = $(this).attr('rel');
				
				if(rel) {
					$(this).parents('.tabs').find('a').removeClass('selected');
					$(this).addClass('selected');
					$('.tabs-content-item').not('#tabs-content-item-'+rel).hide();
					$('#tabs-content-item-'+rel).show();
					e.preventDefault();
				}
			}
		});
		
	}
	
	,init_listings_tools: function() {
		
		$(".listing-keyword-search-keywords").focus(function() {
			if( $(this).val() == $(this).attr('title') ) {
				$(this).removeClass('ghosted').val('');
			}
		}).blur(function() {
			if( $(this).val() == '' ) {
				$(this).addClass('ghosted').val( $(this).attr('title') );
			}
		}).trigger('blur');
		
		$('.listing-keyword-search-button').click(function() {
			if( $(".listing-keyword-search-keywords").val() == '' || $(".listing-keyword-search-keywords").val() == $(".listing-keyword-search-keywords").attr('title') ) {
			} else {
				var keywords = $(".listing-keyword-search-keywords").val();
				    keywords = escape(keywords).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '').replace('@', '%40');
				var county = $('.current_county_slug').val();
				var category = $('.current_category_slug').val();
				if(category) {
					var url = '/find/' + keywords + '/in/' + county + '/' + category + ".html";
				} else {
					var url = '/find/' + keywords + '/in/' + county + '.html';
				}
				website.listings_veil('show');
				window.location = url;
			}
		});
		
		$('.icon-filters input').click(function() {
			$(this).parents('.icon-filters').find('button').fadeIn('normal');
		});
		$('.icon-filters button').click(function() {
			var data = $('.icon-filters:first input').serialize();
			var url  = window.location.href;
			    url  = url.split("?")[0];
			    url += "?" + data;
			website.listings_veil('show');
			window.location = url;
		});
		
		$('.sortby').change(function() {
			var value = $(this).val();
			
			if (window.location.href.indexOf("?") != -1) {
				var url = window.location + "&sortby=" + value;
			} else {
				var url = window.location + "?sortby=" + value;
			}
			
			website.listings_veil('show');
			window.location = url;
		});
		
	}
	
	,listings_veil: function( mode ) {
		
		if( mode == 'show' ) {
			$('.listings-veil').fadeTo('fast', 0.8);
			$('.listings-veil-inner').fadeIn('fast');
		} else {
			$('.listings-veil').fadeOut('fast');
			$('.listings-veil-inner').fadeOut('fast');
		}
		
	}

	,check_if_mobile_device: function() {
		/**
		 * jQuery.browser.mobile (http://detectmobilebrowser.com/)
		 *
		 * jQuery.browser.mobile will be true if the browser is a mobile device
		 *
		 **/
		(function(a){jQuery.browser.mobile=/android|avantgo|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-/i.test(a.substr(0,4))})(navigator.userAgent||navigator.vendor||window.opera);

	}
	
};

$(document).ready(website.init);
