$(function(){
    
    
    $('#search-input input[name="keywords"]').bind('focus',function(){
        if ( $(this).val() == $(this).attr('rel') ) {
            $(this).val("");
        }
    });
    $('#search-input input[name="keywords"]').bind('blur',function(){
        if ( $(this).val() == "" ) {
            $(this).val($(this).attr("rel"));
        }
    });
    
/*
     * Styling
     */
    $('#collectionGallery').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});

    
    /*
     * Pretty forms
     */
    $('form.prettyform input[type*=radio]').css('border','none');
    $('form.prettyform input[type*=checkbox]').css('border','none');
    if($.browser.mozilla) { prettyform(); }
    
    /*
     * LI hover fix for IE6
     */
    $('#navigation ul li').each( function(){
        $(this).mouseover( function(){ $(this).addClass('hover'); } ).mouseout( function(){ $(this).removeClass('hover'); } );
    });
    
    /*
     * Blur on focus
     */
    $('a').focus(function() { this.blur(); });
    $('input[type*=submit]').focus(function() { this.blur(); });
    $('input[type=text], input[type=password]').addClass("input");
    
    $('#navigation ul li:first').addClass('first');
    $('#navigation ul li:last').addClass('last');
    
    $('#collections a:first').addClass('first');
    $('#collections a:last').addClass('last');
    
    $('#subNav ul li:last a').addClass('last');
    
    if( $(".imgMasker").length ){
    	$('.imgMasker').beforeAfter();
    }

	function staticNav() { 
		var sidenavHeight = $("#sidenav").height();
		var winHeight = $(window).height();
		var browserIE6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;

		if (browserIE6) {
			$("#sidenav").css({'position' : 'absolute'});
			$("#header").css({'position' : 'absolute'});
		} else {
			$("#sidenav").css({'position' : 'fixed'});
			$("#header").css({'position' : 'fixed'});
		}
	
		if (sidenavHeight > winHeight) {
			$("#sidenav").css({'position' : 'static'});
		}
	}
	
	//staticNav();
	/*
	$(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
		staticNav();
	});
	*/
	
	// FABRIC MENU
	//$('#collection_menu li:first a').attr
	$('#collection_menu > li > a').addClass('first');
	$('#collection_menu > li > ul > li > a').addClass('second');
	$('#collection_menu > li > ul > li > ul > li > a').addClass('third');
	
	
	$("#collection_menu > li > ul > li").tsort("a[title]",{orderby:"title"});
	
	function checkCollection(){
		//alert($("ul.subnav").text());
		//$("ul.subnav").tsort();
		//
		// Test for passing in the current section
		var locationURL = document.location.toString();
		if (locationURL.match('#')) { // the URL contains an anchor
		  var targetAnchor = locationURL.split('#')[1];
		  testURL = '/Collections/'+targetAnchor;
		  $('#collection_menu > li > a').each(function(){
			  if( $(this).attr('href') == testURL) {
				  $(this).click();
			  }
		  });
		} 
	}

	$('#collection_menu > li > a').bind('click',function(){

		var url = "/Collections";
		//alert( $('#collection_menu').find('li.active').length );
		 if( $('#collection_menu').find('li.active').length || $('#collection_menu').find('a.down').length  ) {
			 currentUrlString = $(this).attr('href');
			 currentUrlString = currentUrlString.split('/');
			 window.location = url+"#"+currentUrlString[2];
			 var location = window.location + '';
			 if (location.indexOf('/Collections/') == -1) {
			     window.location.reload();
			 }
			 //alert('going to ' + url+"#"+currentUrlString[2] );
			 return false;
		 } else { 
			 if(  $(this).siblings('ul').css('display')  != "none" ) {
				 $(this).addClass('down').removeClass('up');
				 window.location = url;
			 } else {
				 $(this).addClass('up').removeClass('down');
			 } 
			
		     $(this).parent().find("ul:first").slideToggle("normal");
		    
		     $(this).parent().siblings().find('ul:first').slideUp("normal");
		     $(this).parent().siblings().find('a.first').addClass('down').removeClass('up');
		     return false;
		 }
	   });
	
	if($('#collection_menu').length) {
		$('ul#collection_menu li ul > li.active:not(ul#collection_menu li ul > li.active ul li.active)').remove().prependTo($('ul#collection_menu li.active > ul '));
		 // appendTo($('ul#collection_menu li.active > ul '));
	}
	checkCollection();
	
	if( ! $("#sub_menu li").length ){
		 $("#wrapper").addClass("headShort");
	}
	 $("#nav li.active").append("<div class='extra' align='center'><img align='center' src='/asset/images/new/menu.gif' /></div>"); 

	
		 $('#gallery').cycle({speed:  2000});
	
    
});


/*
 * Pretty forms
 */
function prettyform(){
  /*
   * Hide forms
   */
  $( 'form.prettyform' ).hide().end();
  /*
   * Label & list formatting
   */
  $( 'form.prettyform' ).find( 'label' ).not( '.noprettyform' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = '';
    this.appendChild( labelSpan );
  } ).end();
  
  /*
   * Show forms
   */
  $( 'form.prettyform' ).show().end();
}
