/*!
 * Copyright 2011 Pebble Design Pty Ltd. All Rights Reserved.
 * All JS libraries copyrighted to their respective owners.
 * Authors: Reiss Watts, Gary Swanepoel //////////////////
 * //////////////////////////////////////////////////////
 * JavaScript: Base.js
 */

/*!
 * Run before HTML is loaded
 */

(function($) {

/*-- AJAX -------------------------------------*/
	
	// Enable caching of AJAX responses
	$.ajaxSetup({
	    cache: true
	});


/* -- IMAGE RESIZE ---------------------------*/
	
	// Function
	$.fn.resizeImage = function (options) {
	    var defaults = {
	        width: '100',
	        height: '100',
	        algorithm: 'fill_proportional',
	        replace: 'TRUE'
	    };
	    return this.each(function () {
	        if (options) {
	            $.extend(defaults, options);
	            var params = '?action=thumbnail&width=' + defaults.width + '&height=' + defaults.height + '&algorithm=' + defaults.algorithm;
	        }
	        $(this).attr('src', function (i, val) {
	            if (defaults.replace == 'TRUE') {
	                val = (val.substring(0, val.indexOf('?')).length > 0) ? val.substring(0, val.indexOf('?')) + params : val + params;
	            } else {
	                val = (val.substring(0, val.indexOf('?')).length > 0) ? val : val + params;
	            }
	            return val;
	        });
	    });
	};

/*-- END --------------------------------------*/

	//alert("END - Base.js");

})(jQuery);

/*!
 * Run after HTML is loaded
 */

$.noConflict();
jQuery(document).ready(function($) {

	//alert("START - Base.js - DOM Ready");
	
//alert("START - Base.onload.js");	

/*-- SLIDESHOW --------------------------------*/

    var height = $('#slideshow .items > div').css('height');
    var width = $('#slideshow .items > div').css('width');
    
    width = width.substring(0, width.indexOf('p'));
    height = height.substring(0, height.indexOf('p'));

    if ((($('.image-manager img').length) !== 0)) {
        $('#slideshow .items').html($('.image-manager > div'));
    }


/*-- IMAGE RESIZE -----------------------------*/

	// Slideshow
	$('#slideshow .items img').resizeImage({
		'width': width,
		'height': height
	});
	
	// Thumbnails
	$('.thumbnail').resizeImage({
		'width': '150',
		'height': '150'
	});



/*-- END --------------------------------------*/	

/*-- SLIDESHOW --------------------------------*/
	
	// Tabs
	$('#slideshow .items > div').each(function (index) {
	    $('#slideshow .navitems').append('<a href="#"></a>');
	});
	
	// Function
	$('#slideshow .navitems').tabs('#slideshow .items > div', {
	    effect: 'fade',
	    fadeOutSpeed: 'slow',
	    fadeInSpeed: 'slow',
	    rotate: true
	}).slideshow({
	    autoplay: true,
	    clickable: false,
	    interval: 3000
	});


/*-- IMAGE RESIZE -----------------------------*/

	// Image Manager
	$('#image-manager img').each(function (index) {
		$(this).resizeImage({
			'width': '100',
			'height': '112'
		}).delay(200 * index).fadeIn(1000);
	});
	

});

jQuery(window).load(function() {

	//alert("START - Base.onload.js - Page Loaded");

/*-- START ------------------------------------*/

	jQuery('.loading').fadeOut(500);

/*-- END --------------------------------------*/

	//alert("END - Base.onload.js - Page Loaded");

});
