/*
*
* default.js
* http://carreirothestudio.com
* francis@francisdaigle.com
*
*/

$(document).ready(function() {
	
	// Preload images
	$.preloadCssImages();
	
	function preload(arrayOfImages) {
	    $(arrayOfImages).each(function(){
	        $('<img/>')[0].src = this;
	    });
	}
	
	// imported generated arrays
	preload(whatsNewImages);
    
    /********************/
	
	// IE 'haslayout()' fix
	if ($('.whats-new').height()) {
		if (jQuery.support.inlineBlockNeedsLayout) {
			$(".whats-new #content #row-top #col-middle .panel-bottom .inner #pagination .control").css("display", "inline");
			$(".whats-new #content #row-top #col-middle .panel-bottom .inner #pagination .control").css("zoom", "1");
		}
	}
	
	/********************/
    
	// Set background image
    var bgCount = 3;

    if ($.cookie("bg")) {
        var cookie = parseInt($.cookie("bg"));
        
        if (cookie != bgCount) {
            newBg = cookie + 1;   
        } else {
            newBg = 1;   
        }
            
        $.cookie("bg", newBg);
    } else {
        var newBg = Math.ceil(Math.random() * 3); 
        $.cookie("bg", newBg);       
    }
    
    var newUrl = "url('/images/default/sections/main/bg-main-" + newBg + ".jpg')";
    $('.main').css ({ 'background-image' : newUrl });
      
    /********************/
    
	// Animate menu events
    $('#menu ul li')
		.hover (
			function(){
				if(!$(this).is(".active")) {
					$(this).stop().animate({backgroundPosition: '(0px 380px)'}, {duration:200});
					$(".active").css( {backgroundPosition: '0px 130px'} );
				} else {
					$(".active").css( {backgroundPosition: '0px 380px'} );
				}
			},
			function(){
				if(!$(this).is(".active")) {
					$(this).stop().animate({backgroundPosition: '(0px 130px)'}, {duration:200});
				}
			}
		);
	$('#menu ul')
		.mouseleave (
			function(){		
				$(".active").css( {backgroundPosition: '0px 380px'} );
			}
		);
	
		
	/********************/
	
	// Jquery accordion
	$.extend($.ui.accordion.animations, {
		customspeed: function(options) {
			$.ui.accordion.animations.slide(options, { duration: 700 }); 
		}
	});
	
	$('#side-left .accordion').accordion({ collapsible: true });
	$('#side-right .accordion').accordion({ collapsible: true, active: false });

	$('.ui-accordion-content-active').css('border-bottom', '1px solid #605F5F');
	
	$('.accordion h3').click(
	   function() {
	       $('.ui-state-default').css('border-bottom', '1px solid #605F5F');
	       $('.ui-accordion-content-active').css('border-bottom', '1px solid #605F5F');
	       
	       if($(this).parent().parent().attr("id") == 'side-left') {
	           $('#side-right .accordion').accordion('activate', false); 
	       } else {
	           $('#side-left .accordion').accordion('activate', false);  
	       }
	   }
    );
	
	$('.accordion').show();
	
	// Accordion scroll
    $('.scroll-pane').jScrollPane({ autoReinitialise: true });
    
});

$(window).load(function() {
	$('.whats-new #content #row-top #col-middle .entry').show();
	$('.whats-new #content #row-top #col-middle #pagination').show();
});
