if(typeof(jQuery) != 'undefined')
{
	jQuery(document).ready(function() {
		jQuery('#gototop').click(function() {
			jQuery('html,body').animate({scrollTop:0}, 'fast'); 
			return false;
		});
		
		jQuery('.menu_home').after(jQuery('#menu_home_body'));
		jQuery('.menu_explore').after(jQuery('#menu_explore_body'));
		jQuery('.menu_articles').after(jQuery('#menu_articles_body'));
		jQuery('.menu_forums').after(jQuery('#menu_forums_body'));
		
		jQuery('#menu_home_body, #menu_explore_body').each(function(item, index) {
			var menuLinks = jQuery(this).find('.tabmenucontent .tabmenuleft ul a');
			var menuLinkBodies = jQuery(this).find('.productoverview');
         
			jQuery.each(menuLinks, function(index, item) {
				jQuery(menuLinkBodies).css('display', 'none');
				jQuery(menuLinkBodies[0]).css('display', 'block');
				jQuery(item).mouseenter(function() {
					jQuery(menuLinkBodies).css('display', 'none');
					jQuery(menuLinkBodies[index]).css('display', 'block');
				});
			});
		});
		jQuery('.menu_home, .menu_explore, .menu_articles, .menu_forums').parent().hover(function() {
			jQuery(this).find('.tabmenu').show();
		},
		function() {
		    jQuery(this).find('.tabmenu').hide();
		});
		jQuery('#customizeteaser').hide();
		
		/* Clicking a link to change the time period on displayed lists of skins */
		jQuery('.tabbed a.tab').click(function () {
			jQuery(this).addClass('selected');
			jQuery(this).siblings().removeClass('selected');

			var index = jQuery(this).index();
			jQuery(this).parent().find('.tabcontent').hide();
			jQuery(this).parent().find('.tabcontent:eq(' + index + ')').show();

			return false;
		});
		
		jQuery('#searchbutton').click(function() {
			var title = jQuery("img.search_bar_icon").attr("title");
			window.location = 'http://www.wincustomize.com/explore/' + title + '/search/' + jQuery('input[title=query]').val().replace(/[^a-zA-Z 0-9]+/g, '').replace(/ /g, '+');

			return false;
		});

		jQuery(".search_menu_button").click(function() {
			jQuery(".dropdownsearch").toggle(200);
		});
		
		jQuery("div.searchmenuitem a").hover(
		function() {
			jQuery(this).next("em").stop(true, true).animate({ opacity: "show", top: "-20" }, "normal");
		},
		function() {
			jQuery(this).next("em").hide();
		});

		jQuery("div.searchmenuitemr2 a").hover(
		function() {
			jQuery(this).next("em").stop(true, true).animate({ opacity: "show", top: "22" }, "normal");
		},
		function() {
			jQuery(this).next("em").hide();
		});

		jQuery("div.searchmenuitemr3 a").hover(
		function() {
			jQuery(this).next("em").stop(true, true).animate({ opacity: "show", top: "65" }, "normal");
		},
		function() {
			jQuery(this).next("em").hide();
		});
		
		jQuery("img.menu_icon").click(
		function() {
			jQuery(".dropdownsearch").hide();
			var imgSrc = jQuery(this).attr("src");
			jQuery("img.search_bar_icon").attr("src", imgSrc);
			var imgName = jQuery(this).attr("title");
			jQuery("img.search_bar_icon").attr("title", imgName);
		});
		
		jQuery('.loginlink').click(function() {
			jQuery('input#_TextUser').focus();
			return false;
		});
	});
}

var Base = Class.create();
Base.Login = Class.create();
Base.Search = Class.create();
Base.IconLegend = Class.create();

Base.setCookie = function(name, value, domain, expiresDays) 
{ 
   expiresDays = (expiresDays * 24 * 60 * 60 * 1000);
   var exp = new Date();
   exp.setTime(exp.getTime() + expiresDays);
   document.cookie = name + "=" + escape (value) + ((exp == null) ? "" : ("; expires=" + exp.toGMTString())) +  ((domain == null) ? "" : ("; domain=" + domain));
}

Base.getCookie = function(name) 
{
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1)
  {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  }
  else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

Base.moveTop = function()
{
   scroll(0,0);
}

Base.removeHtml = function(text, replaceBrackets)
{
   // This line is optional, it replaces escaped brackets with real ones, i.e. < is replaced with < and > is replaced with >
   if(replaceBrackets && replaceBrackets == true) text = text.replace(/&(lt|gt);/g, function (strMatch, p1){ return (p1 == "lt")? "<" : ">"; });
   text = text.replace(/<\/?[^>]+(>|$)/g, '');
   return text;
}

Base.replaceBreaks = function(text)
{
   text = text.replace(/\r\n/gi, '<br/>');
   text = text.replace(/\r\n/gi, '<br/>');
   return text;
}

Base.replaceNewLines = function(text)
{
   text = text.replace(/\r\n/gi, '<br>');
   text = text.replace(/\n/gi, '<br>');
   text = text.replace(/\r/gi, '<br>');
   text = text.replace(/\r\n/gi, '<br/>');
   text = text.replace(/\n/gi, '<br/>');
   text = text.replace(/\r/gi, '<br/>');
   return text;
}

Base.getSelection = function(clear)
{
   var text;
   clear  = clear ? clear : true;
   if (document.getSelection)
   { 
      text = document.getSelection();
      if(text && !(text == '')) window.getSelection().collapseToStart();
   }
   else if (document.selection && document.selection.createRange)
   {
      var range = document.selection.createRange();
      text = range.text;
      range.collapse();
      range = null;
   }
   if(!text) text = '';
   return text;
}


Base.noenter = function()
{
   alert(window.event.keyCode == 13);
  return !(window.event && window.event.keyCode == 13);
}

Base.catchEnter = function(e)
{
   var key;

   if(window.event)
        key = window.event.keyCode;     //IE
   else
        key = e.which;     //firefox

   var result = key != 13;

   return result;
}

// **** Base.Login ****

Base.Login.username = function(item, blur)
{
   if(blur)
      item.value = (item.value == '' ? 'Username' : item.value);      
   else
      item.value = (item.value == 'Username' ? '' : item.value);
}

Base.Login.password = function(item, blur)
{
   if(blur)
      item.value = (item.value == '' ? 'Password' : item.value);      
   else
      item.value = (item.value == 'Password' ? '' : item.value);
}

// **** Base.Search ****

Base.Search.focus = function(item, blur)
{
   if(blur)
      item.value = (item.value == '' ? 'Search' : item.value);      
   else
      item.value = (item.value == 'Search' ? '' : item.value);
}

Base.Search.init = function()
{
   var query = $id('_SearchQuery');
	var search = query.value.trim();
	var csecreator = jQuery(".csecreator").val();
	var cseid = jQuery(".cseid").val();
	
	if(search == '' || search == null)
	{
	   alert('Please enter a search term and try again.');
	   return false;
	}
   
   if (csecreator == '' || csecreator == null || cseid == '' || cseid == null)
   {
      return;
   }
   
   //window.location = '/search/' + search.replace(/[^\w\s]/gi, '');;
   //window.location = 'http://www.google.com/search?q=' + search + ' site:' + location.host;
   window.location = "/googlesearch.aspx?cx=" + csecreator + "%3A" + cseid + "&cof=FORID%3A11&ie=UTF-8&q=" + search + "";
}

Base.Search.initKey = function(e)
{
   if(!Base.catchEnter(e))
   {
      Base.Search.init();
      return false;
   }

   return true;
}

Base.timeDiff = function(fromDate, toDate)
{
	var sec = toDate.getTime() - fromDate.getTime();
	var baseSec = sec;
	
	if (isNaN(sec)){ alert(0); return; }
	if (sec < 0){ alert(1); return; }

	var second = 1000, minute = 60 * second, hour = 60 * minute;

	var hours = Math.floor(sec / hour);
	sec -= hours * hour;
	var minutes = Math.floor(sec / minute);
	sec -= minutes * minute;
	var seconds = Math.floor(sec / second);
	sec -= seconds * second;
	var ms = sec;
	var result = hours + ":" + minutes + ":" + seconds + ':' + ms + ' (' + (baseSec) + 'ms)';

	return result;
}

Base.IconLegend.click = function()
{
	var iconLegendContent = document.getElementById("iconlegendcontent");
	var iconLegendHead = document.getElementById("iconlegendhead");
	if (iconLegendContent.style.display == "none")
	{
		iconLegendContent.style.display = "block";
		iconLegendHead.innerHTML = "Icon Legend - Click To Hide";
	}
	else
	{
		iconLegendContent.style.display = "none";
		iconLegendHead.innerHTML = "Icon Legend - Click To Show";
	}
}
