/*
	Copyright CocciSoftware 2010 
	All right reserved
	http://www.coccisoftware.com

*/

// #################################################################################################

  $jq(document).ready(function() {
	// browser -------------------------------------------------------------------------------------
	//if ($jq.browser.webkit) {
	if ($jq.browser.msie) {
   		//alert("MSIE Sucks!");
   		$jq('#cocciHatesMsie').removeClass('hide');
   		$jq('#cocciHatesMsie').expose({api: true}).load();
   		
 	}

	//-- Tabs --------------------------------------------------------------------------------------
	var $cocciTfx={	api:true, effect: 'fade', fadeOutSpeed: 200, tabs: 'li' };

	if( $jq("#cocciTabs").length){
		var tabs	=$jq("body.cocciSectionIphone #cocciTabs").tabs("div.cocciPanes",	$cocciTfx);
		var tabs	=$jq("body.cocciSectionMac #cocciTabs").tabs("div.cocciPanes",	$cocciTfx);
		var tabs	=$jq("body.cocciSectionAbout #cocciTabs").tabs("div.cocciPanes",	$cocciTfx);
	}
	
	if( $jq("#cocciLinks").length){
		var tabs	=$jq("body.cocciSectionSupport #cocciLinks").tabs("div.cocciPanes",	$cocciTfx);
	}

	$jq('#link-gallery').click(function() { // bind click event to link
		tabs.click(1);
		return false;
	});
	

	//-- Tickers --------------------------------------------------------------------------------------
	
	if( $jq("#cocciNewsTicker div.scrollable").length){
		$jq("#cocciNewsTicker div.scrollable").scrollable({
			loop: true,
			speed: 1000,
			size: 1,
			hoverClass: "hover"
		})
		.navigator({
			navi: "#cocciNewsTickerNav"
		})
		.autoscroll({
			autoplay: true,
			interval:4500
		});
	}
	
	if( $jq("#cocciFoTicker div.scrollable").length){
		$jq("#cocciFoTicker div.scrollable").scrollable({
			vertical:true, 
			loop: true,
			speed: 1000,
			size: 1,
			hoverClass: "hover"
		})
		.navigator({
			navi: "#cocciFoTickerNav"
		})
		.autoscroll({
			autoplay: true,
			interval:2500
		});
	}

	//-- About PR -----------------------------------------------------------------------------------
	if( $jq("body.cocciSectionAbout #cocciGalThumbsV a").length){
		$jq("body.cocciSectionAbout #cocciGalThumbsV a").overlay({ 
			target: '#cocciGalOverlayV', 
			top: -140, 
			left: -530,
			expose: '#f1f1f1' 
		}).gallery({ 
			speed: 500 
		});
	}
	if( $jq("body.cocciSectionAbout #cocciGalThumbsH a").length){
		$jq("body.cocciSectionAbout #cocciGalThumbsH a").overlay({ 
			target: '#cocciGalOverlayH',
			top: -40,
			left: -800,
			expose: '#f1f1f1' 
		}).gallery({ 
			speed: 500 
		});
	}


	//-- Gallery -----------------------------------------------------------------------------------
	if( $jq("#cocciGallery").length){
		cocciGalleryInit('cocciGallery');
	}
	//-- NewsLetter --------------------------------------------------------------------------------
	$jq('#formNlEmail').blur(function()	{ formNlChanged() });
	$jq('#formNlEmail').keyup(function()	{ formNlChanged() });
	$jq('#formNlEmail').change(function()	{ formNlChanged() });

	//-- Contact --------------------------------------------------------------------------------
	if( $jq("#cocciFormContact").length){
		$jq("#cocciFormContact").bind("click keydown", function() { 
			$jq(this).expose({ api: true }).load(); 
		}); 
	}

  });

/*


*/


// #################################################################################################

function formNlChanged() {
	if( $jq('#formNlEmail').val() ){
        $jq('#cocciFormNl dd').addClass('nlChanged');
	}
	else{
        $jq('#cocciFormNl dd').removeClass('nlChanged');
	}
}


// #################################################################################################

cocciGalIndex		=0;
cocciGalTotalWidth	=0;
cocciGalPositions 	= new Array();

function cocciGalleryInit(id){
	// count
	$jq('#'+id+' .slide').each(function(i){
        cocciGalPositions[i]= cocciGalTotalWidth;
        cocciGalTotalWidth += $jq(this).width();
        /* The positions array contains each slide's commulutative offset from the left part of the container */
        if(!$jq(this).width())
        {
            alert("Please, fill in width & height for all your images!");
            return false;
        }
    });
	// set slides div width
	$jq('#'+id+' .cocciGalSlides').width(cocciGalTotalWidth);


	//init links
	if($jq('#'+id+" .cocciGalPrev A")){
		$jq('#'+id+" .cocciGalPrev A").click(function(event){
			cocciGalleryMoveTo(id,cocciGalIndex - 1);
			event.preventDefault();
		});
	}
	if($jq('#'+id+" .cocciGalNext A")){
		$jq('#'+id+" .cocciGalNext A").click(function(event){
			cocciGalleryMoveTo(id,cocciGalIndex + 1);
			event.preventDefault();
		});
	}

	$jq('#'+id+' .cocciGalNav a').each(function(i){
        $jq(this).click(function(event){
			cocciGalleryMoveTo(id,i);
			event.preventDefault();
		});
    });



	//alert("Gal Firered! id="+id+" tw="+cocciGalTotalWidth);
}

//--------------------------------------------------------------------------------------------------
function cocciGalleryMoveTo(id,newIndex){
	var max=cocciGalPositions.length;
    // limit index to a valid position
    if (newIndex < 0) {
		newIndex += max;
    }
    else if (newIndex >= max) {
		newIndex -= max;
    }

	//set transition duration
	var duration = Math.min(Math.abs(newIndex - cocciGalIndex) * 290, 1000);
	// slide it baby
	$jq('#'+id+' .cocciGalSlides').stop().animate({marginLeft:-cocciGalPositions[newIndex]+'px'},duration,'swing');
	
	// set Title, desc , and selected nav
	$jq('#'+id+' .cocciGalNav LI').removeClass('selected');
	$jq('#'+id+' .cocciGalNav LI:eq('+newIndex+')').addClass('selected');
	var current='.cocciGalNav LI.selected';
	$jq('#'+id+' .cocciGalTitle').html($jq(current+' A').html());
	$jq('#'+id+' .cocciGalDesc').html($jq(current+' SPAN').html());
		
	// set current index
	cocciGalIndex=newIndex;

	//alert("index is: " + max);
}
//--------------------------------------------------------------------------------------------------

