$.fn.search = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};


$(function(){
/*
     * Styling
     */
	
	$(".inputSearch").search();
    /*
     * Pretty forms
     */
	$('label.required').prepend(' <em>*</em>'); 
    $('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");
    
    
    $('select#contactList').selectmenu({style:'dropdown'});
    $("#contactList-menu span").each( function(){
    		str = $(this).text();
    		str = str.replace(/-/g,' ' );
    		$(this).text(str);
    });
    
    $("#contactList").change(function(){
        //window.location( $(this).find(':selected').attr('href') );
        if( $("#contactList").find(":selected").attr('rel').length){
              window.location= "/" + $("#contactList").find(":selected").attr('rel');
        }
    });
    
    //$("#navigation > ul").attr('id','nav');
    $("#mainNavigation").superfish({autoArrows: true});  

    $('ul#mainNavigation li ul li a').each(function(){
    	$(this).html( 
        		"<span>"+ $(this).html()+"</span>"
        		);
    });
    
    $('ul#mainNavigation > li').addClass('topLevel');
    
    /* Manually add new window links to external links  */
    $('ul#mainNavigation').find("a[href^='http']").attr('target','_blank');
    
    $('#navigation li:first').addClass('first');
    $('#navigation li:last').addClass('last');
    
    /* Manually remove the news subnav */
    $('ul#mainNavigation > li a').each(function(){
    	if(  $(this).text() == "News" ){
    		$(this).parent('li').find("ul").remove();
    	}
    	
    });
    

    
   //  $('ul#nav li ul li ul').remove();

    if( $("#find_out_about_us").length ) {
    	$("#find_out_about_us").click(function(){
    			checkFindOut();
    		});
    	checkFindOut();
    }
	function checkFindOut(){
		if( $("#find_out_about_us").val() == "Other"){
			$("#other-label,#other-element").show();
		} else {
			$("#other-label,#other-element").hide();
		}
	}
	
	
	if( $("#locationMap").length > 0 ) {
		var latVal = $("#latitude").attr('rel');
		var longVal = $("#longitude").attr('rel');
		
        $("#locationMap").gMap({ markers: 
        		[{ 
        			latitude: latVal,
        			longitude: longVal
        		}]
        		, zoom: 15 
             });
	}
	
	  $("#documentsList li a").mouseover(function(){ 
		  		 if( ! $(this).hasClass('current')) {
		  			 // slide up current
		  			 $("#documentsList li a.current").stop().animate({paddingTop:'5px'},{queue:false, duration:600, easing: 'easeOutBounce'});
		  			 // remove current class
		  			 $("#documentsList").find('a.current').removeClass("current");
		  			 
		  			 // slide down this
		  			 $(this).stop().animate({paddingTop:'20px'},{queue:false, duration:600, easing: 'easeOutBounce'});
		  			 // add current class
		  			 $(this).addClass('current');
		  			
		  		 } 
		     });  
	  
	  $("#documentsList li a").mouseout(function(){  
	       //  $(this).stop().animate({paddingTop:'46px'},{queue:false, duration:600, easing: 'easeOutBounce'});
	     }); 	  
	
	//alert(  $.cookie("coverflow") );
	if( $.cookie("coverflow") != null && $.cookie("coverflow") == "hidden" ){
		$("div.coverflow").css("height","0px");
		$("#slideButton").text('Show');
		$("#slideButton").css('background-position','bottom');
		$("#slideButton").addClass('show');
		if ($.browser.msie && $.browser.version.substr(0,1)<8) {
			 $("div#flashwide").css("height","0px");
		}
	}
	$("#slideButton").click(function(){
		if( $(this).hasClass('show')){
			
			//alert('show');
			if ($.browser.msie && $.browser.version.substr(0,1)<8) {
				$("div.coverflow").css("height","375px");
				$("div#flashwide").css("height","375px");
			} else {	
				$("div.coverflow").animate({
					height: "460px"
				})
				.animate({
					height: "375px"
				},
				"fast" 
					 , function(){ 
						//$("div.coverflow,#flashwide").css("height","388px"); 
						//alert( $("div.coverflow").css('height') ); 
						
					} 
				);
			}
			$(this).removeClass('show');
			$(this).text('Hide');
			$(this).css('background-position','top');
			//$("div.coverflow");
			$.cookie("coverflow", "shown", { expires: 14 });
		
		} else {
			//alert('hide');
			if ($.browser.msie && $.browser.version.substr(0,1)<8) {
				 $("div.coverflow").css("height","0px");
				 $("div#flashwide").css("height","0px");
			
			} else {
				$("div.coverflow").animate({
					height: "460px"
				})
				.animate({
					height: "0px"
				},
				"fast" 
					, function(){ 
						//$("div.coverflow,#flashwide").css("height","0px"); 
						
						//$("#slideButton").show();
						//alert( $("div.coverflow").css('height') ); 
					
					} 
				);
			}
			$(this).addClass('show');
			$(this).text('Show');
			$(this).css('background-position','bottom');
			//$("div.coverflow")
			$.cookie("coverflow", "hidden", { expires: 14 });
			
		}
	
	});	
	
	if( $("div.scrollable").length ){
		  $(".scrollable").scrollable({circular: false, size: 3,clickable:false });
		 var api = $(".scrollable").data("scrollable");
		// api.next();
		 // console.dir($(".scrollable").data("scrollable"))
		 // $(".scrollable").data("scrollable").getIndex()
		
		
	}
	if($("div.galBlock").length ){
		$("div.galBlock").hover(
				  function () {
					    $(this).addClass('hover')
					  }, 
					  function () {
						  $(this).removeClass('hover')
					  }
					);
	}
	$("div.galBlock").each(function(index){
		var $targetImg = $(this).find('a.img');
		
		$(this).find('div.galTitle a').click(function(){
			$targetImg.click();
		});
		
		$(this).find('div.icon').click(function(){
			$targetImg.click();
		});
		
	});
	/*
	 $("div.galBlock div.galTitle a, div.galBlock div.icon").click(function(){
	 	   $(this).parents('div').find('a.img').click();
	 	   return false;
	  });
		
		$("div.galBlock").click(function(event){
		 	   $(this).children('a.img').click();
		 	   event.stopPropagation();
		 	   return false;
		 	   // TODO fix recursion error
		 });
		 */
	  /*
	    $("div.galBlock div.galTitle a, div.galBlock div.icon").click(function(){
	 	   $(this).parents('div').find('a.img').click();
	 	   return false;
	   });
	    */
	  
	   
	    $("#borchureForm fieldset, #resourceUpdateForm fieldset").each( function(index){
	    	$(this).prepend("<div class='addCorners'><h2>"+$(this).find('legend').text()+"</h2></div>");    	
	    	$(this).find('legend').remove(); //$(this).text('test');
	    });
	
	if( $(".addCorners").length ){
		$(".addCorners").append('<div class="cnrTpLeft" />');
		$(".addCorners").append('<div class="cnrTpRight" />');
		$(".addCorners").append('<div class="cnrBtmLeft" />');
		$(".addCorners").append('<div class="cnrBtmRight" />');
	}	
	
	$('.swatch').click(function(){
		// console.log('click ' + $('p#swatch').text());
		$('#colour_desc').text($('p#swatch').text());
	});
	if( $('.swatch').length ) {
		$('#swatches div:first').click();
		$('#colour_desc').text($('#swatches div:first').attr('title'));
	}
	
	$('.scroll-pane').jScrollPane();

	// customsolutionsnz Vscroll
	if( $(".vscrollable").length ) {
	  $(".vscrollable").scrollable({ vertical: true, size: 2,clickable:false });
	}

	
});



/*
 * 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();
}