// JavaScript Document

$(document).ready(function(){   
	initDropdownMenuOnHover('audio');
	initDropdownMenuOnHover('tv');
	initDropdownMenuOnHover('kitchen');
	initDropdownMenuOnHover('delivery');
	initDropdownMenuOnHover('services');
	initDropdownMenuOnHover('store');
	initDropdownMenuOnHover('contact');
	initDropdownMenuOnHover('lihome');
	initDropdownMenuOnHover('computing');
	initDropdownMenuOnHover('camera');
	//initNotificationThing();
	
	//for any ajax forms
	jQuery('form #js').val('yes');

	initQuickSearch();
	//initCommentForm();
	initTabs();
	LoadContent('review');
	
	
	
	
	// only clears the field only once on focus
	jQuery('.clear_field').one("focus", function() {
	  jQuery(this).val("");
	});


	function initQuickSearch() {
		var loading_url = '/javascript/jquery/ajax/loadingimages/snake.gif'; 
		var egg = $(".text_tiptoe").attr("value");
		
		$("input[type='text'].text_tiptoe").change( function() {
		//$('.text_tiptoe').change(function() {
			var selected_category = $(".search select").attr("value");
			var search_term = $(".text_tiptoe").attr("value");
		  jQuery('.product_results_count').html('<div class="loading"><img src="'+  loading_url +'"/></div>');												  
			//$.get("/search.php?keywords=" + search_term + '&category_string=' + selected_category, function(data){
			//	jQuery('.product_results_count').html(data);
			//});
		});
		$("select").change( function() {
		//$('.text_tiptoe').change(function() {
			var selected_category = $(".search select").attr("value");
			var search_term = $(".text_tiptoe").attr("value");
		  jQuery('.product_results_count').html('<div class="loading"><img src="'+  loading_url +'"/></div>');												  
			//$.get("/search.php?keywords=" + search_term + '&category_string=' + selected_category, function(data){
			//	jQuery('.product_results_count').html(data);
			//});
		});
		
		/*$('input.tiptoe').bind('click', function() {
			var selected_category = $(".search select").attr("value");
			var search_term = $(".text_tiptoe").attr("value");
			jQuery('.product_results_count').html('<div class="loading"><img src="'+  loading_url +'"/></div>');												  
			$.get("/search.php?keywords=" + search_term + '&category_string=' + selected_category, function(data){
				jQuery('.product_results_count').html(data);
			});
			return false;
		});*/
	}


	
	function initDropdownMenuOnHover(cssclass) {
		 jQuery("li." + cssclass).mouseenter(function(){
			jQuery('li.' + cssclass + ' .nav_dropdown').css("display","block");
 			jQuery('li.' + cssclass).addClass("hover");
			
			if ( $.browser.msie ) {
				var browser_version = $.browser.version;
				if (browser_version < 8) {
					jQuery('.cycle_slides').cycle('pause');
					jQuery('.cycle_slides').hide();
				}
			}
			
		 }).mouseleave(function(){
			jQuery('li.' + cssclass + ' .nav_dropdown').hide();
			jQuery('li.' + cssclass).removeClass("hover");
			
			if ( $.browser.msie ) {
				var browser_version = $.browser.version;
				if (browser_version < 8) {
					jQuery('.cycle_slides').cycle('resume');
					jQuery('.cycle_slides').show();
				}
			}			
		 })
	};
	
	
	function initCommentForm() {
		jQuery('.form_submit').bind('click', function() {
			jQuery.ajax({
				type: "POST",
				url: "/comment_process.php",
				data: jQuery("form.comment_form"),
				success: saveUserResponse
			});
			return false;
		});
		// what happens on success of submitting form
		function saveUserResponse(){
			jQuery('.your-comments').html('<p>Thankyou for submitting your comment</p>');
		}
	};
	
	function initTabs() {
		jQuery(".tab-container").each(function(){
			var $tabContainer = jQuery(this),
				$tabItemSet = $tabContainer.find('.tab-items'),
				$tabItems = $tabItemSet.find('.tab-item'),
				$tabHeadings = $tabContainer.find('.tab-headings'),
				$tabs,
				$headingLink;
				
			if($tabContainer.hasClass('tab-bottom')){
				$tabItemSet.after('<div class="tab-tabs"></div>');
			} else {
				$tabItemSet.before('<div class="tab-tabs"></div>');
			}
			
			$tabs = $tabContainer.find('.tab-tabs');
			
			$tabItems.each(function(){
				var $tabItem = jQuery(this),
					$tabHeading = $tabItem.find('.tab-heading');
				$tabHeading.wrapInner('<a></a>');	
				
				$tabHeading.children().each(function(){
					$headingLink = jQuery(this);
					$headingLink.click(function(){//add the click listener
						$tabs.find('.tab-heading').removeClass('tab-current');
						$tabHeading.addClass('tab-current');
						$tabItems.removeClass('tab-current');
						$tabItem.addClass('tab-current');
						return false;
					});
				});
				$tabHeading.appendTo($tabs); //move the tab heading to the tab container
			});
			$tabContainer.addClass('tab-loaded');
		});
	}
	
	
	function LoadContent(content) {
		if (content == 'review') {
			jQuery('a.load_reviews').bind('click', function() {
				var currentId = $('div.load_reviews').attr('id');
				jQuery.get("/external.php?ext_cont=" + content + "&product_id=" + currentId +"", function(data){
					jQuery('a.load_reviews').remove();																														
					jQuery('div.load_reviews').append(data);
				});
			});
		}
	}
	
	function initNotificationThing() {
		jQuery('.delivery_notification .submit_button').css('border','1px solid #ccc');
		jQuery('.delivery_notification .submit_button').bind('click', function() {
			jQuery.ajax({
				type: "POST",
				url: "/basket.php",
				data: jQuery("form.delivery_notification").serialize(),
				success:  function(data){
					if (data == 'fail') {
						jQuery('.notification').removeClass('success');
						jQuery('.notification_text').empty();
						jQuery('.notification_text').load('/helpers.php?url_id=delivery_not_available .helper_content');
						jQuery('.checkout_btn').addClass('grey_button');
						jQuery('.checkout_btn').attr('href', '#');
						jQuery('.default_message').hide();
						jQuery('.notification_text').show();
					} else if(data == 'pass') {
						jQuery('.notification').addClass('success');
						jQuery('.notification_text').empty();
						jQuery('.notification_text').load('/helpers.php?url_id=delivery_available .helper_content');
						jQuery('.checkout_btn').removeClass('grey_button');
						jQuery('.checkout_btn').attr('href', '/checkout.php');
						jQuery('.default_message').hide();
						jQuery('.notification_text').show();
					}
				}
			});
			return false;
		});
	};
	

	
	
});
