$(function(){
	function url_decode(url) {
		// fixed -- + char decodes to space char
		var o = url;
		var binVal, t, b;
		var r = /(%[^%]{2}|\+)/;
		while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
		if (m[1] == '+') {
		t = ' ';
		} else {
		b = parseInt(m[1].substr(1), 16);
		t = String.fromCharCode(b);
		}
		o = o.replace(m[1], t);
		}
		return o;
		}
	
/*
     * Styling     */
    
    /*
     * 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 li").each(
    		function(){
    			$(this).append('<span>\\</span>');
    });
    $("#navigation li span:last").remove();
    
    $('#slides') 
    .before('<div id="imgNav">') 
    .cycle({ 
        fx:     'fade', 
        speed:  'slow', 
        timeout: 3000, 
        pager:  '#imgNav',
        before:  onBefore, 
        after:   onAfter 
        
    });    
    
    function onAfter() { 
    	
        $('#caption').html('<h3>' + this.alt + '</h3>');
    }
    function onBefore() { 
        $('#caption').html('<h3>' + this.alt + '</h3>');
    }   
    
    
    $('#gallery img:gt(0)').hide();

    $('#gallery').cycle({ 
        fx:     'fade', 
        speed:  'fast', 
        timeout: 0, 
        next:   '#galnext', 
        prev:   '#galprev',
        delay:   -2000,
        after:   onAfterGal

    });    
    
 // callback fired when each slide transition begins    
    function onAfterGal(curr,next,opts) {
    	var caption = 'Image ' + (opts.currSlide + 1) + ' of ' + opts.slideCount;
    	$('#GalleryInfo').html(caption);
        var $slide = $(next);
        var w = $slide.outerWidth();
        var h = $slide.outerHeight();
        $slide.css({
           // marginTop: (400 - h) / 2,
            marginLeft: (560 - w) / 2
        });
        
    }
    

	if( $("#locationMap").length > 0 ) {
		var latVal = $("#latitude").attr('rel');
		var longVal = $("#longitude").attr('rel')
        $("#locationMap").gMap({ markers: 
        		[{ 
        			latitude: latVal,
        			longitude: longVal
        		}]
        		, zoom: 15 
             });
		// get the address        
		var location  = new GLatLng(latVal,longVal);
		geocoder = new GClientGeocoder();
		geocoder.getLocations(location, showAddress);
		
    }
	function showAddress(response) {
			if (!response || response.Status.code != 200) {
			    return false;
			  } else {
				  locationAddress = response.Placemark[0].address;
				  $("#mapLink").html('<a href="http://maps.google.com/maps?saddr=&daddr='+locationAddress+'" target="_blank">Get directions to here</a>');
			  }

	}
	
    
    if( $("#contactMap").length > 0 ) {
        $("#contactMap").gMap({ markers: 
        		[{ 
        			latitude: -37.733059,
        			longitude: 175.274087
        		}]
        		, zoom: 15 
             });
        if( document.URL.indexOf('?') > 0 ){
        	$("#contact_details").hide();    
        	$("#contact_details").before("<p><a class='toggleMore' href='javascript:void(0);'>Show Details</a></p>");
        	
        	
    		var locationURL = document.location.toString();
    		if (locationURL.match('=')) { // the URL contains an anchor
    		  var targetVar = locationURL.split('=')[1];
    		  $("#note").val("Interested in: " + url_decode(targetVar) );
    		}
        }
    }
    
    $(".toggleMore").toggle(function(){
    	$(this).parent().next("div").slideDown();
    	$(this).text("Hide Details");
    	},function(){
    	$(this).parent().next("div").slideUp();
    	$(this).text("Show Details");
    });
    
    $(".inlineVideo").colorbox({width:"680", height:"400", inline:true, href:"#video"});
    $("a[rel='slideshow']").colorbox({slideshow:true});



});


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