parseUri.options = {
  strictMode: false,
  key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
  q:   {
     name:   "queryKey",
     parser: /(?:^|&)([^&=]*)=?([^&]*)/g
  },
  parser: {
     strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
     loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
  }
};

function parseUri( str ) {
  var   o   = parseUri.options,
        m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
     uri = {},
     i   = 14;

  while (i--) uri[o.key[i]] = m[i] || "";

  uri[o.q.name] = {};
  uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
     if ($1) uri[o.q.name][$1] = $2;
  });

  return uri;
};

function extractURL( k ) {
  return parseUri( document.location.href )[k];
}
/*
//examples
alert( "source : "    + extractURL("source") );
(available: protocol, authority, userInfo, user, password, host, port, relative, path, directory, file, query, anchor)
*/
function openAnswer() {
   anchorID = extractURL("anchor");
   if( anchorID != "" )
   {
      $("dt#" + anchorID).next("dd").slideDown(300);
   }
}         


function highlightItem(item) {
  var normalColor = $(item).css('backgroundColor');
  $(item).animate({
    'backgroundColor': "#fec",
    'opacity':'1'
    },
    { queue: false, duration: 500 });
    
  setTimeout( function(){  
    $(item).animate(
        { 'backgroundColor': normalColor},
        { 'queue'          : false,  'duration': 800 }
    );
  }, 2000);
}



// toggles FAQs
function toggleFaqs() {
	$("dl.faq dd").hide(); //hide all answers by default
	$("dl.faq dt").click(function() {
	  $(this).next().slideToggle(300);
	});
}

// toggles terms and rules in registration form
function toggleTerms() {
	$("#terms-wrap, #rules-wrap").hide();
	var termsFieldSet = $("#lbl_terms_information_fieldset");
	if(termsFieldSet.length > 0) {
		$("#terms-wrap").appendTo(termsFieldSet);
		$("#rules-wrap").appendTo(termsFieldSet);
		$("#rules_accepted_label a.terms").click(function(){
			$("#rules-wrap").hide();
		    $("#terms-wrap").slideToggle();
		    return false;
		});
		$("#rules_accepted_label a.rules").click(function(){
		    $("#terms-wrap").hide();
		    $("#rules-wrap").slideToggle();
		    return false;
		});
	}
}

// vertically centers all thumbnails in catalog
// fails when images don't specify height attr
function centerThumbs() {
	$("a.thumbWrap img").each(function(){
	    var imgHeight = 0;
	    imgHeight = $(this).height();
	    $(this).css("margin-top", (110 - imgHeight)/2);
	});
}


$(function(){
	
	$("#topnav").superfish( {autoArrows: false, dropShadows: false, delay: 400, animation: {opacity:'show',height:'show'} } );

	$("#topCategories").superfish( {dropShadows: false, delay: 400 } );

	var anchorID = "";
	openAnswer();
	
	anchorID = extractURL("anchor");
	if (anchorID != "") {
	  document.location.replace( "#"+anchorID );
	}
	
	toggleFaqs();
	toggleTerms();

//	centerThumbs();

	if ($("a.launchvid").length) {
		$("a.launchvid").colorbox({iframe:true, innerWidth:640, innerHeight:360})
	}

});

