/*

Scripts written by Alex Dickson for Acura Multimedia (http://www.acura.com.au)

All scripts leverage the jQuery framework (http://www.jquery.com) by John Resig (http://www.ejohn.org)

*/


// global stuff 
var GOOGLE_ANALYTICS_KEY = 'UA-693909-3';

// add this class to specify .javascript * in css for quicker css application

$('html').addClass('javascript');

// on DOM ready

$(document).ready(function(){

	$('html').addClass('dom-loaded');

    // make side things go to bottom
    
    sideTiles();

    menu();
	
	
	    
    accordion();

    
     if ($('body').hasClass('real-weddings')) {
    
        $('#text > ul > li').each(function() {
        
        	var id = $(this).attr('id');
        	
        	var $h3Anchor = $(this).find('h3 a');
        	
        	var $anchors = $(this).find('.examples a').add($h3Anchor);
        	
        	
        	
        	$anchors.attr({ rel: id }).fancybox({
        		cylic: true
        	});
        	
        	
        	$(this)
        		.find('img')
        		.css({ cursor: 'pointer' })
        		.click(function() {
        			$h3Anchor.click();
        		});

        	        
        });
    
    };
    
   
    if ($('body').hasClass('book-online')) { 
      
      var date = new Date();
      
      var currentYear = date.getYear();
        
      var yearMin = currentYear;
      var yearMax = currentYear + 1;  
        
      $.datepicker.setDefaults({dateFormat: 'dd/mm/yy', yearRange: yearMin + ':' + yearMax, constrainInput: true, minDate: new Date()});
       $('body.book-online #text form input.date').datepicker();
    }
    
    if ($('body').hasClass('google-map-area')) {
    
         function LoadMapSearchControl() {

          var options = {
                zoomControl : GSmapSearchControl.ZOOM_CONTROL_ENABLE_ALL,
                title : "Ikatan Spa",
                url : "http://www.ikatanspa.com",
                idleMapZoom : GSmapSearchControl.ACTIVE_MAP_ZOOM-2,
                activeMapZoom : GSmapSearchControl.ACTIVE_MAP_ZOOM-2
                }
    
          new GSmapSearchControl(
                document.getElementById('google-map'),
                "46 Grays Road – Doonan – QLD – 4562",
                options
                );
    
        }
        // arrange for this function to be called during body.onload
        // event processing
        GSearch.setOnLoadCallback(LoadMapSearchControl);
    
    };
    
    if ($('body').hasClass('image-gallery')) {
        
      

        //slideShow.init();
        //slideShow.lim();
    }
    
    // new links in window
    
    $('a[rel=external], .links #text a').not('.real-weddings #text a').newWindow();
    
    highlightFormSuccess();
    
    // Simple confirmation on gift cert
    
    $giftCertForm = $('.gift-certificates form:first');
	$giftCertForm.find('button').html('Proceed');
    $('#input-gift-certificate-amount').change(function() { 
    
    	$('#cert-confirmation').fadeOut(250, function() { $(this).remove(); });
		$giftCertForm.find('button').html('Proceed');
    });
    
    $giftCertForm.submit(function(event) {
    	
    	
  
    	
    	if ( ! $('#cert-confirmation').length) {
    		$(this).append('<div id="cert-confirmation"><p></p><p>Click the button above again to confirm.</p>');
    	    event.preventDefault();
			$(this).find('button').html('Confirm Purchase');
    	};
    	
    	var giftCertAmount = $('#input-gift-certificate-amount').val();
    	
    	// Add leading $ if not present
    	if ( ! giftCertAmount.match(/^\$/)) giftCertAmount = '$' + giftCertAmount;
    	
    	$('#cert-confirmation p:first').text('You are about to order a gift certificate for ' + giftCertAmount  + '.');
    	
    });
    
});


// on window loaded



$(window).load(function(){  // window load

	$('html').addClass('window-loaded');

	//google analytics

	try {

		var pageTracker = _gat._getTracker(GOOGLE_ANALYTICS_KEY);

		pageTracker._trackPageview();

	} catch(err) {}

});

var sideTiles = function() {
    
    var newHeight = $('#main-container').css('height');
    
    $('#tile-left, #tile-right').css( { height: newHeight } );
    
}

var menu = function() {

    $('#spa-menus strong').click(function() {
        
        $subMenu = $(this).parent().find('ul');
        $subMenu.is(':visible')  ? $subMenu.slideUp(500) : $subMenu.slideDown(500);
    
    });


};

var highlightFormSuccess = function() {
    
    var fadeTo = 0.6;
    
    var $info = $('p.information');
    
    if ($info.length) {
        
    
        // make overlay
        
        var $overlay = $('<div />')
            .css({
                display: 'none',
                width: '100%',
                height: $(document).height(),
                position: 'absolute',
                top: 0,
                left: 0,
                zIndex: 32760,
                opacity: 0
            })
            .attr({
                id: 'overlay'
            })
            .appendTo('body');
        
     
        
        $info
            .css({
                position: 'relative',
                zIndex: 32767
            });
        
        $overlay
        .show()
        .fadeTo(1000, fadeTo);
        
        
        // wait then fade back out
        setTimeout(function() {
        
            $($overlay).fadeOut(1000, function() {
                $(this).remove();
            });
        
        }, 2500);
    
    
    };
    

};

var accordion = function() {

    $('body.book-online form .treatment-booking h3, body.shop-online form h3').each(function() {
        
        $(this).prepend('<span class="toggle-icon">show</span>')
        
        var $toggleIcon = $(this).find('.toggle-icon');
        
        var $inputContainer = $(this).siblings('.input-container');
        
        $(this).click(function() {
        
            if ($inputContainer.is(':visible')) {
                $inputContainer.slideUp(500, function() { 
                    $toggleIcon.text('show');
                    sideTiles();

                 });
            } else {
                $inputContainer.slideDown(500, function() {
                    $toggleIcon.text('hide')
                    sideTiles();

                });
            };
            

        });
    
    });

};

