////////////////////////////////////////////////////////////////////////////////////
// image pre-loader
jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++)   {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$.preloadImages("/_images/denimatrix_color.png",
                "/_images/close.png",
                "/_images/dot_white_80p.png");

////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function() { 


    $(document).pngFix(); //Activate pngFix()

    ////////////////////////////////////////////////////////////////////////////////////
    // Start delayed fade-in of color logo
    setTimeout(function() {
    	$("#logo").ImageSwitch({  
       		Type: "FadeIn",
       		NewImage: "/_images/denimatrix_color.png",
       		Speed: 3000
   	})
    }, 1500);


    ////////////////////////////////////////////////////////////////////////////////////
    // Set content opening/closing

    var totalMenuTabs = 5; 

    function openContent(tab) {
    	
    	if ($('#tab'+tab).hasClass('selected')) {
    		//if the content is currently open, close it
    		closeContent(tab);
    	}
    	else {
    		//otherwise, close any open content and open the new content

	  	for (i = 1; i < (totalMenuTabs + 1); i++) {
			$('#tab'+i).removeClass('selected');
    			$('#tab'+i+'_content').hide();
  		}	
		$('#tab'+tab).addClass('selected');
		$('#tab'+tab+'_content').show('normal')  	
    	}  
    }
    
    function closeContent(tab) {
    	$('#tab'+tab+'_content').hide('fast');
	$('#tab'+tab).removeClass('selected');
    }       	

    $('a#tablink1').click(function() {openContent(1);});
    $('a#tablink2').click(function() {openContent(2);});
    $('a#tablink3').click(function() {
    	openContent(3);

	$('#viSlides').cycle({
        	fx:     'scrollDown',
        	speed:  '1500',
        	timeout: 10000,
        	cleartype:  1, // enable cleartype corrections         
        	pager:  '#viNav',
        	pagerEvent: 'mouseover',        
        	pagerAnchorBuilder: function(idx, slide) {
            		// return sel string for existing anchor
		        return '#viNav li:eq(' + (idx) + ') a';
        	}
	    });    
    });
    $('a#tablink4').click(function() {openContent(4);});
    $('a#tablink5').click(function() {openContent(5);});

    $('a#close1').click(function() {closeContent(1);});
    $('a#close2').click(function() {closeContent(2);});
    $('a#close3').click(function() {
    	closeContent(3);
    	$('#slideshow').cycle('stop');
    });
    $('a#close4').click(function() {closeContent(4);});
    $('a#close5').click(function() {closeContent(5);});            

    ////////////////////////////////////////////////////////////////////////////////////
    // start site background slideshow 
    $('#background-image').cycle({ 
	timeout: 5000,  // milliseconds between slide transitions (0 to disable auto advance) 
    	speed:   2000,  // speed of the transition (any valid fx speed value) 
        before:   onBefore 
    }); 
 
    var totalSlideCount = 19; 
     
    function onBefore(curr, next, opts) { 
        // on the first pass, addSlide is undefined (plugin hasn't yet created the fn); 
        // when we're finshed adding slides we'll null it out again 
        if (!opts.addSlide) 
            return; 
 
        // on Before arguments: 
        //  curr == DOM element for the slide that is currently being displayed 
        //  next == DOM element for the slide that is about to be displayed 
        //  opts == slideshow options 
             
        var currentImageNum = parseInt(next.src.match(/background(\d+)/)[1]); 

        if (currentImageNum == totalSlideCount) { 
            // final slide in our slide slideshow is about to be displayed 
            // so there are no more to fetch 
            opts.addSlide = null; 
            return; 
        } 
         
        // add our next slide 
        opts.addSlide('<img src="_images/backgrounds/background'+(currentImageNum+1)+'.jpg" width="900" height="600" />'); 
    }; 



    ////////////////////////////////////////////////////////////////////////////////////
    // round content area corners	
    if (jQuery.support.opacity) {
	$('.tabarea').corner('7px');
    }

    
    ////////////////////////////////////////////////////////////////////////////////////
    // clients hover-zoom effect 
    $("ul.clSlides li").hover(function() {
	$(this).css({'z-index' : '10'});
	$(this).find('img').addClass("hover").stop()
		.animate({
			marginTop: '-80px', 
			marginLeft: '-105px', 
			top: '50%', 
			left: '50%', 
			width: '200px', 
			height: '150px',
			padding: '5px' 
		}, 200);
	
	} , function() {
	$(this).css({'z-index' : '0'});
	$(this).find('img').removeClass("hover").stop()
		.animate({
			marginTop: '0', 
			marginLeft: '0',
			top: '0', 
			left: '0', 
			width: '180px', 
			height: '130px', 
			padding: '5px'
		}, 400);
    });

}); 


