searchFrmViewState = function() {
		$('#formContainer').fadeOut();
    }
                
    arrowTog = function() {
		$("#arrow").toggleClass("downArrow");
    }

	$.fn.clearForm = function() { 
        return this.each(function() { 
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form') {
            return $('div#advancedSearchSection :input', this).clearForm();}
        if (type == 'text' || type == 'password' || tag == 'textarea')
           {this.value = '';}
        else if (type == 'radio' && this.id=="radExact")
            { this.checked = false; $('#radEither').attr('checked', true);}
        else if (tag == 'select')
            { this.selectedIndex = 0;}
        });
    };
    
    frmReset = function() {
		//$('#frmAdvSearch').get(0).reset();
		$('div#advancedSearchSection :input').clearForm()
        $('#keyword').val('Search Videos/Recipes');
        disableCategories();
        $('#cb_allCategories, #cb_all').attr('checked','checked').removeAttr('disabled');
        hideRecipeFilters();
        $('#cb_title').removeAttr('checked').attr('disabled',true);
    }
    


	disableCategories = function() { 
		$('#cb_recipe, #cb_exercise, #cb_weightLoss, #cb_slimmer, #cb_health, #cb_rcWorld, #cb_shop').removeAttr('checked').attr('disabled',true);
    }

	enableCategories = function() {
		$('#cb_recipe, #cb_exercise, #cb_weightLoss, #cb_slimmer, #cb_health, #cb_rcWorld, #cb_shop').attr('disabled',false);
    }

	disableFindResultsIn = function() {
		$('#cb_title, #cb_ingredients').removeAttr('checked').attr('disabled',true);                         
	}

	enableFindResultsIn = function() {
		$('#cb_title, #cb_ingredients').attr('disabled',false);                        
    }

	hideRecipeFilters = function() {
		$('#recipeFilters').fadeOut('medium');
		$('#cb_ingredients').removeAttr('checked').attr('disabled',true);
	}

	showRecipeFilters = function() {
		$('#recipeFilters').fadeIn('medium');
		$('#cb_ingredients').removeAttr('checked').attr('disabled',true);
	}

	// functions ready for execution on document ready state
	$(document).ready(function() {
        //alert($.browser.version);
        var browsername;        
        if($.browser.safari)
          browsername = "Safari";
          
		$('#searchResults').jScrollPane();
		
		// toggle the display of default text of Searchbox on focus & off focus
		$('#keyword').each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				if(this.value == default_value) {
					this.value = '';
				}
			});
			$(this).blur(function() {
				if(this.value == '') {
					this.value = default_value;
				}
			});
		});
		
		// hide the advanced search section on page load
		searchFrmViewState();
		
		// enable 'All Categories' in Categories & Find Results In, 'All' checkbox on page load
		$('#cb_allCategories, #cb_all').attr('checked','checked');                               
		
		// on page load disable category filters
		disableCategories();
		
		// on page load disable find results in filters
		disableFindResultsIn();
		
		// on page load hide recipe filters
		hideRecipeFilters();
		
		// highlight "All Categories" on page load
		$("label[for='cb_allCategories'], label[for='cb_all']").addClass('txtHighlight');
		
		//checked
		var checked = $('input[type=checkbox]').is(':checked');
		// unchecked
		var unchecked = $('input[type=checkbox]:checked').val() == undefined;

		// toggle the visibility of the advanced search filters
		$("#searchContainer").click(function (){
			$("#formContainer").slideToggle("medium");
			arrowTog();
		});
		
		$("#btnAdvSearch, #btnGoSearch").click(function(event) {
    		event.preventDefault();
    		
    		if(this.id == "btnGoSearch"){ 
			    $("#formContainer").slideDown("medium");
			    arrowTog();
			    $('#loadingSearch').fadeIn("medium");
			    
		    } else{
			    $('#loadingSearch').fadeIn("medium");
		    }
    		
			if($('#arrow').hasClass('rightArrow')){
				 $("#arrow").addClass("downArrow");		   
			}

			$('.playerLayout #bottomBorder').css('background','none');
			
			$.ajax({  
				type: "GET",
				url: "/search.aspx",
				data: $("#advancedSearchSection input, #advancedSearchSection select").serialize(),  
				
				error: function() {
					$('#searchResults').html('<li>No results found</li>');
				},
				success: function(data) {
				    var htmlFound = $(data).find('#searchContent');
					$('#formContainer').fadeIn();
					$('#searchFilters, #navButtons').fadeOut('fast');
					$('.mainLayout #advancedSearchSection .jScrollPaneContainer, .playerLayout #advancedSearchSection .jScrollPaneContainer').css('display','block');
					$('#searchResults').css('display','block');
					$('#searchResults, #navButtonsReloaded').slideDown("medium");
					$('#searchResults').html(htmlFound);
					$('#searchResults').jScrollPane({
			            showArrows:true,
			            maintainPosition: false,            			
			            scrollbarWidth: 16,
			            reinitialiseOnImageLoad: true, 
			            scrollbarMargin:4, 
			            arrowSize: 16, 
			            dragMinHeight:16, 
			            dragMaxHeight :16, 
			            scrollbarOnLeft: false
		            });
		            
					$('div#advtitle').replaceWith("<div id='advtitle'>Search Results</div>");	
					$('#searchContent div.videoTitle').ellipsis();			
					$('#searchContent p').ellipsis();
					$('#loadingSearch').fadeOut("medium");
				
				}  
			}); 
		});
		
		$('#keyword').keypress(function(event) {
            if(event.which == 13) { 
                event.preventDefault();                   
                                
                $("#formContainer").slideDown("medium");
                arrowTog();
                $('#loadingSearch').fadeIn("medium");
        
                if($('#arrow').hasClass('rightArrow')){
                    $("#arrow").addClass("downArrow");                      
                }

                $('.playerLayout #bottomBorder').css('background','none');
            
                $.ajax({  
				    type: "GET",
				    url: "/search.aspx",
				    data: $("#advancedSearchSection input, #advancedSearchSection select").serialize(),  
    				
				    error: function() {
					    $('#searchResults').html('<li>No results found</li>');
				    },
				    success: function(data) {
				        var htmlFound = $(data).find('#searchContent');
					    $('#formContainer').fadeIn();
					    $('#searchFilters, #navButtons').fadeOut('fast');
					    $('.mainLayout #advancedSearchSection .jScrollPaneContainer, .playerLayout #advancedSearchSection .jScrollPaneContainer').css('display','block');
					    $('#searchResults').css('display','block');
					    $('#searchResults, #navButtonsReloaded').slideDown("medium");
					    $('#searchResults').html(htmlFound);
					    $('#searchResults').jScrollPane({
			                showArrows:true,
			                maintainPosition: false,            			
			                scrollbarWidth: 16,
			                reinitialiseOnImageLoad: true, 
			                scrollbarMargin:4, 
			                arrowSize: 16, 
			                dragMinHeight:16, 
			                dragMaxHeight :16, 
			                scrollbarOnLeft: false
		                });
    		            
					    $('div#advtitle').replaceWith("<div id='advtitle'>Search Results</div>");	
					    $('#searchContent div.videoTitle').ellipsis();			
					    $('#searchContent p').ellipsis();
					    $('#loadingSearch').fadeOut("medium");
    				
				    }
			    });
            }
        });

		
		$('#btnSearchAgain').click(function() {
			$('.mainLayout #advancedSearchSection .jScrollPaneContainer, .playerLayout #advancedSearchSection .jScrollPaneContainer').css('display','none');
			$('div#advtitle').replaceWith("<div id='advtitle'>Advanced Search</div>");
			$('#searchResults, #navButtonsReloaded').slideUp('fast');                                           
			$('#navButtons, #searchFilters').slideDown('medium');
		});
		
		$('#btnClose').click(function() {
			$('#formContainer').slideUp('medium');
			arrowTog();
		});
		
		$('#btnAdvSearch').hover(function() {
			$('#btnAdvSearch').toggleClass('navBtnSearchHover');
		});
		
		$('#btnReset').hover(function() {
			$('#btnReset').toggleClass('navBtnResetHover');
		});
		
		$('#btnSearchAgain').hover(function() {
			$('#btnSearchAgain').toggleClass('navBtnSearchAgainHover');
		});
		
		$('#btnClose').hover(function() {
			$('#btnClose').toggleClass('navBtnCloseHover');
		});
		
		// function for resetting the values of the form on click of Reset button
		$('#btnReset').click(function() {
			frmReset();
			$("label").removeClass('txtHighlight');
			$("label[for='cb_allCategories'], label[for='cb_all']").addClass('txtHighlight');
		});
		
		// function for enabling & disabling other categories & dropdown filters based on the categorie filter selected
		$(function() {
			$('#cb_recipe').click(function() {                                                               
				var enable_cb_exercise = $('#cb_recipe:checked').val();
				var enable_cb_weightLoss = $('#cb_recipe:checked').val();
				var enable_cb_slimmer = $('#cb_recipe:checked').val();
				var enable_cb_health = $('#cb_recipe:checked').val();
				var enable_cb_rcWorld = $('#cb_recipe:checked').val();
				var enable_cb_shop = $('#cb_recipe:checked').val();
				var enable_cb_allCategories = $('#cb_recipe:checked').val();
				
				if ((enable_cb_exercise != undefined) || (enable_cb_weightLoss != undefined) || (enable_cb_slimmer != undefined) 
					|| (enable_cb_health != undefined) || (enable_cb_rcWorld != undefined)  || (enable_cb_shop != undefined) 
					|| (enable_cb_allCategories != undefined)){

					showRecipeFilters();
					$('#loadingSearch').css('height','313px');
					if($.browser.msie && ($.browser.version == "7.0")){$('#loadingSearch').css('height','345px');}
                    if(browsername == "Safari"){$('#loadingSearch').css('height','288px');}
                    
					if(($('#cb_title').attr('checked')) || (!($('#cb_title').attr('checked')) && !($('#cb_all').attr('checked')))){
						$('#cb_ingredients').removeAttr('disabled');
					   }
					$('#cb_exercise, #cb_weightLoss, #cb_slimmer, #cb_health, #cb_rcWorld, #cb_shop, #cb_allCategories').removeAttr('checked').attr('disabled', true);
				}
				else {
					hideRecipeFilters();
					$('#loadingSearch').css('height','265px');
					if($.browser.msie && ($.browser.version == "7.0")){$('#loadingSearch').css('height','286px');}
					if(browsername == "Safari"){$('#loadingSearch').css('height','245px');}
					$('#cb_exercise, #cb_weightLoss, #cb_slimmer, #cb_health, #cb_rcWorld, #cb_shop, #cb_allCategories').removeAttr('disabled');
				}
			});
                                                
			$('#cb_allCategories').click(function(){
				var enableAllCategories = $('#cb_allCategories:checked').val();
				if(enableAllCategories != undefined) {
					$('#cb_allCategories').attr('checked','checked');
					disableCategories();
				}
				else {
					$('#cb_allCategories').removeAttr('checked');
					enableCategories();
				}
			});
													
			$('#cb_all').click(function() {
				var enableTitle = $('#cb_title:checked').val();
				var enableIngredients = $('#cb_ingredients:checked').val();                                                                                                        
				if (((enableTitle != undefined) || (enableIngredients != undefined)) || ($('#cb_all').attr('checked'))) {
					$('#cb_title, #cb_ingredients').removeAttr('checked').attr('disabled', true);                                                                        
				}
				else {  
					$('#cb_title, #cb_ingredients').removeAttr('disabled');                                                                  
				}
			});
													
			$('#cb_title').click(function() {
				var enableTitle = $('#cb_title:checked').val();
				var enableIngredients = $('#cb_ingredients:checked').val();
				var enableAll = $('#cb_all:checked').val();
				
				if ((enableTitle != undefined) || (enableIngredients != undefined)) {
					$('#cb_all').removeAttr('checked').attr('disabled', true);
				}
				else {
					$('#cb_all').removeAttr('disabled');
				}
			});
													
			$('#cb_ingredients').click(function() {
				var enableTitle = $('#cb_title:checked').val();
				var enableIngredients = $('#cb_ingredients:checked').val();
				var enableAll = $('#cb_all:checked').val();
				
				if ((enableTitle != undefined) || (enableIngredients != undefined)) {
					$('#cb_all').removeAttr('checked').attr('disabled', true);
				}
				else {
					$('#cb_all').removeAttr('disabled');
				}
			});
                                
		});
                                
		// highlight checkbox text on check of checkbox
		$('input[type=checkbox]').click(function(){ 
			$('input[type=checkbox]').each(function(){               
				lid = $(this).attr('id'); 
				if ($(this).attr('checked')) {  
					$('label[for='+lid+']').addClass('txtHighlight');
				}
				else {
					$('label[for='+lid+']').removeClass('txtHighlight');
				}
			});
		});
	});
	
