jQuery.fn.exists = function(){
  return jQuery(this).length>0;
};

$(document).ready(function(){
    
	//check text only enabled
    textOnlySwitcher()
    
    // text only control
    $("#textonlycontrol").click(function(){
    	textOnlyToggle()
    });
    
    // Only run these when not in text only mode

    if($.cookie('text_only')!= 'on'){
	    //print function
	    $("a.print").click(function(){
	    	 window.print();
	  		 return false;
	    });
	    
	    // PA & Job Mailer form setup
	    $('.request_contact').click(function(){
	    	external_id = this.toString();
	    	external_id = external_id.substring(external_id.indexOf('#')+1,external_id.length);
	    	$('#pamailer #external_id').val(external_id)    	
	    });
	    
	    //dialog box
	    $(".dialog").hide();
	    $(".dialog_control").click(function(){   	    	
	    	$(".dialog").dialog();
	    });
	       
	    //product gridview toggle
	    current_url = removeURLParam(window.location.toString(),"view");	   
	     $('#view_mode #list_link').attr("href", current_url+"&view=list")
	     $('#view_mode #grid_link').attr("href", current_url+"&view=grid")
	     
	    //Show hide for view
		$(".collapsible").click(function(){	
		  $(this).next("div").slideToggle("slow")
		  .siblings("div:visible").slideUp("slow");
		  $(this).toggleClass("active");
		  $(this).siblings("h2").removeClass("active");	
		});
		
		
		
		//Results dynamic clear facet button
		var pathname = window.location.toString().replace(/\?.*/, "");
		$("#search_extras").append("<a class='clear_filters facet_static' href='"+pathname+"'>Clear Search</a>");
		
		//Flash Message time out
		setTimeout(function(){
		  $("#flash_message").fadeOut("slow", function () {
		    $("#flash_message").remove();
		  });
		}, 5000);
		
		//tiny mce include
		if($(".wyeditor").exists()){
			$('.wyeditor').tinymce({			
			   script_url : 'includes/js/tinymce/tiny_mce.js',		
			   theme : "simple"	
			});		
			$("form").submit(function(){
				tinyMCE.triggerSave(true,true);
			});	
		}
		
		//Tabs
		$(".tabs").tabs({ fx: { opacity: 'toggle', duration: 300 } });
	
		//Search results hover fade
		if (!$.browser.msie || parseInt($.browser.version, 10) > 8){
	      var onMouseOutOpacity = 1;
	      $('div#hits > div.onehit').css('opacity', onMouseOutOpacity)
	      .hover(
	      function () {
	        $(this).prevAll().stop().fadeTo('slow', 0.50);
	        $(this).nextAll().stop().fadeTo('slow', 0.50);
	      },
	      function () {
	        $(this).prevAll().stop().fadeTo('slow', onMouseOutOpacity);
	        $(this).nextAll().stop().fadeTo('slow', onMouseOutOpacity);
	      }
	      );
	    }
			
	    // Clear search box text
			$(".defaultText").focus(function(srcc) {
				if ($(this).val() == $(this)[0].title) {
					$(this).removeClass("defaultTextActive");
					$(this).removeClass("greytext");
					$(this).val("");			
				}
			});
		
			$(".defaultText").blur(function() {
				if ($(this).val() == "") {
					$(this).addClass("defaultTextActive");
					$(this).addClass("greytext");
					$(this).val($(this)[0].title);
				}
			});
		
			$(".defaultText").blur();  
		
			$("form").submit(function() {
				
				$('.defaultText').each(function(index){
					if ( $(this).val() ==  $(this).attr('title')) {$(this).val("");}			
				})
			});
			
			$(".defaultText").click(function() {
				if ( $(this).val() ==  $(this).attr('title')) {$(this).val("");}
			});	
						
			
   		//facet functions
		  $('.hiddenfacet').hide();
		  
		  $('div.facet').each(function() {
			$('.hiddenfacet:last', this).after('<li><a class="facetshow" href="#" title="More">More&hellip;</a></li>');
			$('.facetshow', this).toggle(function() {
			  $('.hiddenfacet', $(this).parent().parent()).slideDown("fast");
			  $(this).attr('title', 'Less');
			  $(this).html('Less&hellip;');
			}, function() {
			  $('.hiddenfacet', $(this).parent().parent()).slideUp("fast");
			  $(this).attr('title', 'More');
			  $(this).html('More&hellip;');
			});
		  });
		
		  if($('.facet_list :last').hasClass('available_facet_label')){
				$('.available_facet_label').hide();
			}
		
	    
	    /*Mega Menu Only when not in text only mode*/  
	    jQuery('#mega-menu').dcMegaMenu({rowItems: '5'});
		
		/* DD Rounded Corners */
		DD_roundies.addRule('.corner_all', '10px');
		DD_roundies.addRule('.corner_tl', '10px 0 0 0');
		DD_roundies.addRule('.corner_tr', '0 10px 0 0');
		DD_roundies.addRule('.corner_top', '10px 10px 0 0');
		
		/* DD Rounded Corners */
		DD_belatedPNG.fix('#strapline');
		
    }
    
    /*text only styles*/
    $('#darktheme').click(function(){
    	darktheme()
    	$.cookie('text_theme','darktheme')
    });
    
    $('#lighttheme').click(function(){
    	lighttheme()
    	$.cookie('text_theme','lighttheme')
    });
    
    $('#normaltext').click(function(){
    	normaltext()
    	$.cookie('text_size','normal')
    });
    
    $('#largetext').click(function(){
    	largetext()
    	$.cookie('text_size','large')
    });
});

function textOnlyToggle(){	
	
	if($.cookie('text_only') != null){
		//switch the status by deleting the cookie and refresh the page
		$.cookie('text_only',null);
		$.cookie('text_theme',null)
		$.cookie('text_size',null)
		location.reload();
	}else{
		$.cookie('text_only','on')
		location.reload();		
	}
    textOnlySwitcher()  
}

function textOnlySwitcher(){

	if($.cookie('text_only') == 'on'){
		$('head link[rel="stylesheet"]').remove();
		$('#textonlycontrol').remove();
		$('head #textonly').attr('rel','stylesheet');		
		$('body').prepend("<a id='textonlycontrol' href='#'>[Graphical Version]</a>&nbsp;");
		$('body').prepend("<a id='largetext' href='#'>[Large Text Size]</a>&nbsp;");
		$('body').prepend("<a id='normaltext' href='#'>[Normal Text Size]</a>&nbsp;");
		$('body').prepend("<a id='darktheme' href='#'>[Dark Theme]</a>&nbsp;");
		$('body').prepend("<a id='lighttheme' href='#'>[Light Theme]</a>&nbsp;");
		lighttheme()
		normaltext()
	}
				

	//Handle theme
	theme = $.cookie('text_theme');
	if(theme == 'darktheme'){
		darktheme()
	}else if(theme == 'lighttheme'){
		lighttheme()
	}
	
	//Handle Size
	size = $.cookie('text_size');
	if(size == 'normal'){
		normaltext()
	}else if(size == 'large'){
		largetext()
	}
}

function darktheme(){
	$('body').css('background','#000');
    $('body').css('color','#00E1E8');
    $('body a').css('color','#FFFF00');
}

function lighttheme(){
	$('body').css('background','#fff');
    $('body').css('color','#000');
    $('body a').css('color','#025699');
}

function normaltext(){
	$('body').css('font-size','100%');	
}

function largetext(){
	$('body').css('font-size','175%');
}

function removeURLParam(url, param)
{
 var urlparts= url.split('?');
 if (urlparts.length>=2)
 {
  var prefix= encodeURIComponent(param)+'=';
  var pars= urlparts[1].split(/[&;]/g);
  for (var i=pars.length; i-- > 0;)
   if (pars[i].indexOf(prefix, 0)==0)
    pars.splice(i, 1);
  if (pars.length > 0)
   return urlparts[0]+'?'+pars.join('&');
  else
   return urlparts[0];
 }
 else
  return url;
}



