/***************************************************************
jQuery also coded by David Lazaro | Creative Kuya Graphic Design
ynawmtalkimbout - CreativeKuya.com
****************************************************************/

//The OTHER slideshow
var slideCount = 0;		//global variable

function otherfadedSlideshow() {
	slideCount = (slideCount + 1);	//increment the slide count
	
	var slideArray = $(".slideList li") //assigns last slide value
	var howManyImg = slideArray.length; //determine how many images are in that array
	var theListItem = $(slideArray[slideCount]);	//get the current list item
	var theLink = $("a", theListItem).attr('href');
		
	if ((howManyImg - 1) == slideCount) { slideCount= -1; } //if you're at the last one, go back to the beginning

	var mainSlide = $(".slides li:last"); //assigns last slide value
	var subSlide = $(".slides li:first");	//assigns first slide value

	$("img", subSlide).attr('src', theLink).load( function() {	//loads the next image into subSlide
		$(mainSlide).stop().fadeTo(1000,0, function() {		//fades the current top slide
			$(mainSlide).prependTo(".slides");	// move to the back	
			$(mainSlide).css({'opacity':'1'});	// reset opacity to fully opaque
			resizeBGimg();	//resize
			setTimeout(otherfadedSlideshow, 7000);	});//hold on for 10 seconds till the next
		});	
}

//mouse over - come into existence
function mmMouseOver() {
	$(this).stop().fadeTo(100, 0.93 );
	$("h2", this).stop().fadeTo(100, 0.93 );
	$(".extraPop", this).stop().animate({'opacity':'1', 'left':'100%'}, 150);

}

function mmMouseOut() {
	$(this).stop().fadeTo(100, 0.75 );
	$("h2", this).stop().fadeTo(100, 0 );
	$(".extraPop", this).stop().animate({'opacity':'0', 'left':'133%'}, 250);
}

//TWITTER latest tweet retrieve
function twitterCallback() {	
	var username='MyNinjaClothing'; // set user name
	var format='json'; // set format, you really don't have an option on this one
	var url='http://api.twitter.com/1/statuses/user_timeline/'+username+'.'+format+'?callback=?'; // make the url
	$.getJSON(url,function(tweet){ // get the tweets
		$("#lastTweet").html(tweet[0].text); // get the first tweet in the response and place it inside the div
	});
}

//BLOG latest blog post retrieve
function blogCallback() {	
	$("#lastBlog").load('myninja/index.php .post-title:first', function() {	//load the first post title
		$("#blogPop h3#page_title").css({'padding':'0px'}).css({'margin':'0px'});	//format it, specific for bathala project
	});	
}


/***************
2. Image resizer
****************/
	
function resizeLaunchFont() {	//resizes the mm fonts
	var w = $(window).width();
	var h = $(window).height();

	var launch = (h*.2);
	var launchFont = (launch*.75);
	$(".mmItem h2").css({ 'font-size': launchFont, 'padding-top': '0px'});
	
	var contactFont = (launchFont*.8);
	$("#contact h2").css({ 'font-size': contactFont, 'padding-top': '5px'});
		
	var popHeader = (launchFont*.3);
	$(".extraPop h3").css({ 'font-size': popHeader});
		
	var popTxt = (popHeader*.4);
	$(".extraPop").css({ 'font-size': popTxt});
	$("#blogPop h3#page_title").css({ 'font-size': popTxt}).css({'padding':'0px'}).css({'margin':'0px'});
		
}

function resizeLaunchMobile() {	//resizes the mm fonts for MOBILE device
	var w = $(window).width();
	var h = $(window).height();
	var winRatio = (w/h);

	var launch = (h*.2);
	var launchFont = (launch*.75);
	$(".mmItem h2").css({ 'font-size': launchFont, 'padding-top': '0px'});
	
	var contactFont = (launchFont*.8);
	$("#contact h2").css({ 'font-size': contactFont, 'padding-top': '5px'});
		
	if (winRatio <= 1.02) {	//if the user is on a vertically-biased window/screen, like a mobile phone
		$('#launchPad').css({'width':'50%','left':'25%'});	//increase the width of the #launchPad
		var mobiLaunch = (launch*.45);	//resize the mainmenu h2 as well so it fits
		$(".mmItem h2").css({ 'font-size': mobiLaunch, 'padding-top': '0px'});
	}
	else {	//otherwise... if the user resizes to let width be greater than height
		$('#launchPad').css({'width':'33.33%','left':'33.33%'});	//reset the width of the #launchPad
	}
		
}

function resizeBGimg() {
	var w = $(window).width();
	var h = $(window).height();
	var img_h = $('.mainImg').height();
	var img_w = $('.mainImg').width();
	if((img_h<h && w<img_w) || w < 3*h/2){
		$(".theShow").addClass("resize"); }
	else{ $(".theShow").removeClass("resize"); }		
}
	
function autoResizeWindow(){
	var w = $(window).width();
	var h = $(window).height();
	if(w < 3*h/2){
		$(".theShow").addClass("resize");
	}	
}

/*******
3. Wait Until Our Assets Are Available (wait till everything is loaded)
**********************************************************************/
$(window).ready(function (){
	$("#bgNoJava").css({ 'display':'none'});	//hide the jquery-unfriendly stuff

	//resizing of elements
	autoResizeWindow();	//resize bg img out the gate and if the window resizes
	resizeBGimg();	//start off by resizing the bg to the browser
	$(window).resize(resizeBGimg);	//if the user resizes/zooms in, resize the bg again	
	

	//slideshow stuff
	$(".slides li").css({'position':'absolute'});	//for multiple slides
	setTimeout(otherfadedSlideshow, 7000);	//do the slide show 
	
	var deviceAgent = navigator.userAgent.toLowerCase();	//look at the device
	var agentID = deviceAgent.match(/(iphone|ipod|ipad|android)/);	//check if it's a mobile device
	
	if (agentID) {	//if it is a mobile device... 
        $(".mmItem").css({ 'opacity': '.9' });	//set opacity of mmitems 
		resizeLaunchMobile();	//resize launchpad font height
		$(window).resize(resizeLaunchMobile);	//if user resizes/zooms in, resize the fonts and shit
	}
	
	else {	//otherwise, if on a regular desktop, do the following...
		
		//hide stuff and reveal stuff		
		$(".extraPop").css({ 'display':'inline', 'opacity': '0', 'left':'133%'});			
		//set opacities of mm	
		$(".mmItem").css({ 'opacity': '.75' });
		$(".mmItem h2").css({ 'opacity': '0'});
			
		//re-color .extraPop h3's
		$("#shop h3").css({ 'color': '#d9893a' });
		$("#blog h3").css({ 'color': '#e5cc36' });
		$("#twitter h3").css({ 'color': '#5d98d2' });		
	
		resizeLaunchFont();	//resize launchpad font height
		$(window).resize(resizeLaunchFont);	//if user resizes/zooms in, resize the fonts and shit	
	
		$(".mmItem").hover(mmMouseOver, mmMouseOut);	//main menu hover states
		
		twitterCallback();	//retrieve latest tweet	
		blogCallback();
	}
});
