var BLOGGER_TICKER = "";

function stopAnimations(layers){
	$.each(layers, function(index, value) {
    	value.stop(true, true);
   	});
}

function switchToState(layers, startCss, destCss, time) {
    $.each(layers, function(index, value) {
    	if (value.is(':animated')) {
    	value.stop().switchClass(startCss[index], destCss[index], time);
    	} else {
    		value.switchClass(startCss[index], destCss[index], time);
    	}
   	});
}

function swapOpacity(src,dest,time) {
	if (jQuery.support.opacity) {
		if (src.is(':animated')) {
			src.stop().fadeTo(time, 0);
			dest.stop().fadeTo(time, 1);
		} else {
			src.fadeTo(time, 0);
			dest.fadeTo(time, 1);
		}
	} else {
		src.css('visibility', 'hidden');
		dest.css('visibility', 'visible');
	}
}

function showrecentposts(json) {
	var numposts=7;
	var numchars=350;
	var myFeed = "";
	for (var i = 0; i < numposts; i++) {
		var entry = json.feed.entry[i];
		var posttitle = entry.title.$t;
		var posturl;
		if (i == json.feed.entry.length) break;
		for (var k = 0; k < entry.link.length; k++) {
		    if (entry.link[k].rel == 'alternate') {
        		posturl = entry.link[k].href;
        		break;
      		}
    	}
    	var postdate = entry.published.$t.substring(0,10);
    	var postauthor = entry.author[0].name.$t;

    	if ("content" in entry) {
      		var postcontent = entry.content.$t;
      	} else if ("summary" in entry) {
      		var postcontent = entry.summary.$t;
      	} else { 
      		var postcontent = "";
      	}
    	var re = /<\S[^>]*>/g; 
    	postcontent = postcontent.replace(re, "");
    	if (postcontent.length > numchars) postcontent = postcontent.substring(0,numchars);
		myFeed += '<p class="genevaMedium">'+ posttitle + '<p>';
    	myFeed += '<p class="genevaSmall"> Posted by: '+ postauthor + ' at ' + postdate +'</p>';
    	myFeed += '<p class="helveticaSmall">'+ postcontent + '...</p>';
    	myFeed += '<a href="'+ posturl +'" class="genevaSmall" style="color:#d03f2f;">(Read More)<a>';
    	myFeed += '<br/></br></br>';
  	}
  	BLOGGER_TICKER = myFeed;
}


function loadAjaxContent(url, el){
	el = '#'+el;
	$(el).empty();
	$(el).load(url + ' #draggable_content', function(){
    	$('#draggable_content').draggable({
    		axis: 'x',
    		cancel: ''
    	});
    });
    return false;
}



$(document).ready(function() {

	jQuery.easing.def = 'easeInOutCubic';

	$.preloadCssImages();

    var state_layers = new Array($('#erba'),$('#albero'),$('#albero_offset'),$('#colline'),$('#montagne'));
    var state_home = new Array('h_erba','h_albero','h_albero_offset','h_colline','h_montagne');
    var state_creations = new Array('cr_erba','cr_albero','cr_albero_offset','cr_colline','cr_montagne');
    var state_about = new Array('ab_erba','ab_albero','ab_albero_offset','ab_colline','ab_montagne');
    var state_news = new Array('nw_erba','nw_albero','nw_albero_offset','nw_colline','nw_montagne');
    var state_current = state_home;
    
    var state_menu_layers = new Array($('#aboutBtn'),$('#newsBtn'),$('#creationsBtn'),$('#contactsBtn'));
    var state_menu_home = new Array('h_aboutBtn','h_newsBtn','h_creationsBtn','h_contactsBtn');
    var state_menu_creations = new Array('cr_aboutBtn','cr_newsBtn','cr_creationsBtn','cr_contactsBtn');
    var state_menu_about = new Array('ab_aboutBtn','ab_newsBtn','ab_creationsBtn','ab_contactsBtn');
    var state_menu_news = new Array('nw_aboutBtn','nw_newsBtn','nw_creationsBtn','nw_contactsBtn');
    var state_menu_current = state_menu_home;
    
    var state_content_layers = new Array($('#info_right'), $('#content_right'), $('#content_left'));
    var state_content_home = new Array('h_info_right', 'h_content_right', 'h_content_left');
    var state_content_creations = new Array('cr_info_right', 'cr_content_right', 'cr_content_left');
    var state_content_about = new Array('ab_info_right', 'ab_content_right', 'ab_content_left');
    var state_content_news = new Array('nw_info_right', 'nw_content_right', 'nw_content_left');
    var state_content_current = state_content_home;

	$('#container').fadeTo(1000,1);
	
	$('#sub_tronco').draggable({
		axis: 'y'
	});
	
	$('#info_right_content').draggable({
		cursor: 'move',
		revert: true,
		revertDuration: 500,
		containment: 'document'
	});
	
	

/******************************************************/
/*                     MENU LOGIC                     */
/******************************************************/

    $('#aboutBtn').click(function(){
    	if (state_current == state_creations) {
    	   	$('#submenu').animate({
    			left: '-=120'
    		}, 1000);
    	}
        if (state_current == state_news || state_current == state_creations) {
      		$('#content_left').empty();
      	}
		if (state_current != state_about) {
			switchToState(
				state_layers.concat(state_menu_layers, state_content_layers),
				state_current.concat(state_menu_current, state_content_current),
				state_about.concat(state_menu_about, state_content_about),
				1000
			);
			state_current = state_about;
			state_menu_current = state_menu_about;
			state_content_current = state_content_about;
			$('#content_left').load('about.html #aboutBox', function(){
				$('#aboutBox').draggable({
					cursor: 'move',
					containment: 'window'
				});
			});
		}
        return false;
    });

    $('#creationsBtn').click(function(){
    	if (state_current != state_creations) {
    		switchToState(
    			state_layers.concat(state_menu_layers, state_content_layers),
				state_current.concat(state_menu_current, state_content_current),
				state_creations.concat(state_menu_creations, state_content_creations),
				1000
			);
    		state_current = state_creations;
    		state_menu_current = state_menu_creations;
    		state_content_current = state_content_creations;
    		$('#content_right').load('alice.html #draggable_content', function(){
    			$('#draggable_content').draggable({
    				axis: 'x',
    				cancel: ''
    			});
    		});
    		$('#submenu').animate({
    			left: '+=120'
    		}, 1000);
    	}
        return false;
    });

    $('#homeBtn').click(function(){
    	if (state_current == state_creations) {
    	   	$('#submenu').animate({
    			left: '-=120'
    		}, 1000);
    		$('#content_left').empty();
    	}
    	if (state_current != state_home) {
    		switchToState(
    			state_layers.concat(state_menu_layers, state_content_layers),
				state_current.concat(state_menu_current, state_content_current),
				state_home.concat(state_menu_home, state_content_home),
				1000
			);
      		state_current = state_home;
    		state_menu_current = state_menu_home;
    		state_content_current = state_content_home;
    	}
    	return false;
    });
    
    $('#newsBtn').click(function(){
    	if (state_current == state_creations) {
    	   	$('#submenu').animate({
    			left: '-=120'
    		}, 1000);
    	}
      	if (state_current == state_about || state_current == state_creations || state_current == state_home) {
      		$('#content_left').empty();
      	}
    	if (state_current != state_news) {
			switchToState(
				state_layers.concat(state_menu_layers, state_content_layers),
				state_current.concat(state_menu_current, state_content_current),
				state_news.concat(state_menu_news, state_content_news),
				1000
			);
			state_current = state_news;
			state_menu_current = state_menu_news;
			state_content_current = state_content_news;
			$('<div id="blog_ticker" style="cursor:move;">').appendTo('#content_left');
			$(BLOGGER_TICKER).appendTo('#blog_ticker');
			$('#blog_ticker').draggable({
				axis: 'y',
				delay: 70
			});
		}
	    return false;
    });

    $('#contactsBtn').click(function(){
	    window.location = 'mailto:info@zakuroart.com';
	    return false;
    });

/******************************************************/
/*                      MENU GFX                      */
/******************************************************/

    $('#aboutBtn').hover(function(){
		swapOpacity($('#aboutGlyphNormal'), $('#aboutGlyphOver'), 300);
	  	return false;
    }, function() {
    	swapOpacity($('#aboutGlyphOver'), $('#aboutGlyphNormal'), 300);
    	return false;
   	});
   	
   	$('#newsBtn').hover(function(){
		swapOpacity($('#newsGlyphNormal'), $('#newsGlyphOver'), 300);
	  	return false;
    }, function() {
    	swapOpacity($('#newsGlyphOver'), $('#newsGlyphNormal'), 300);
    	return false;
   	});
   	
   	$('#creationsBtn').hover(function(){
		swapOpacity($('#creationsGlyphNormal'), $('#creationsGlyphOver'), 300);
	  	return false;
    }, function() {
    	swapOpacity($('#creationsGlyphOver'), $('#creationsGlyphNormal'), 300);
    	return false;
   	});
   	
   	$('#contactsBtn').hover(function(){
		swapOpacity($('#contactsGlyphNormal'), $('#contactsGlyphOver'), 300);
	  	return false;
    }, function() {
    	swapOpacity($('#contactsGlyphOver'), $('#contactsGlyphNormal'), 300);
    	return false;
   	});
});
