// JavaScript Document

var x, y, bWidth, bHeight = 0;
var gutter = 30;
var bgIndex = 0;
var minHeight = 760;
var minWidth = 980;

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays===null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name){
	var c_start, c_end;
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) { c_end=document.cookie.length; }
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function recolor(obj) {
	var color = obj.attr("class");
	// change main bar color
	$("#bedo-bar").removeClass().addClass(color);	
	//update cookie for refresh
	setCookie('colorbar', color,365);
}

function checkCookie(){
	var color=getCookie('colorbar');
	if (color!==null && color!==""){
		recolor($("#color-"+color));
	} else {
		setCookie('colorbar',"blue",365);
	}
}

function parseXML(xml) {
  //find every Tutorial and print the author
	var counter=0;
  $(xml).find("post").each(function() {
		if(counter<3) {
			var entryDate = new Date($(this).find("date:first").text());
			var pubMonth = entryDate.getMonth()+1;
			switch (pubMonth) {
				case 1 : pubMonth = "January"; break;
				case 2 : pubMonth = "February"; break;
				case 3 : pubMonth = "March"; break;
				case 4 : pubMonth = "April"; break;
				case 5 : pubMonth = "May"; break;
				case 6 : pubMonth = "June"; break;
				case 7 : pubMonth = "July"; break;
				case 8 : pubMonth = "August"; break;
				case 9 : pubMonth = "September"; break;
				case 10 : pubMonth = "October"; break;
				case 11 : pubMonth = "November"; break;
				case 12 : pubMonth = "December"; break;
			}
			var pubDay = entryDate.getDate();
			var pubYear = entryDate.getFullYear();							
																			
			var postHTML = "<li class='post'>";
			postHTML += '<div class="post-title"><h2>'+ pubMonth + ' '  + pubDay + '<span>' + pubYear+'</span></h2></div>';
			if($(this).find("title").text() != "Untitled") { postHTML += '<h4>' + $(this).find("title").text() + '</h4>'; }
			postHTML += '<p>'+ $(this).find("body:first").text() +'</p>';
			postHTML += "</li>";
			$("#posts").append(postHTML);
			counter ++;
		}
  });

}

function reSize () {
	//get browser size
	bWidth = $(window).width();
	bHeight =  $(window).height();
	if(bWidth<=minWidth){ bWidth = minWidth; } 
	if(bHeight<=minHeight){ bHeight = minHeight; } 
	$('body').height(bHeight);
	var sWidth = bWidth*3 + gutter*3;
	var sHeight = bHeight *2;

	
	//set width + height for container for 100% height
	$("#container").width(bWidth).height(bHeight);
	$("#screens-container").width(sWidth).height(sHeight);
	$(".screen-container").width(bWidth).height(bHeight);	
}

$(document).ready(function() {
	checkCookie();
	reSize();
  
	$(window).resize(reSize);
	
	//on menu click
	$("#menu a").click(function (){
		$("#menu a").removeClass();

		var newX = 0;
		var newY = 0;
		switch ($(this).attr("id")) {
			case "menu-home" : $(this).addClass("menu-home-on"); document.title = 'Home + BeDo'; break;
			case "menu-about" : newX = bWidth+gutter; $(this).addClass("menu-about-on"); document.title = 'About + BeDo'; break;
			case "menu-work" : newX = bWidth*2+gutter*2; $(this).addClass("menu-work-on");document.title = 'Work + BeDo'; break;
			case "menu-talks" : newY = bHeight; $(this).addClass("menu-talks-on");document.title = 'Talks + BeDo'; break;
			//case "menu-blog" : newY = bHeight; $(this).addClass("menu-blog-on");document.title = 'Blog + BeDo'; break;
			case "menu-goods" : newX = bWidth+gutter; newY = bHeight; $(this).addClass("menu-stuff-on");document.title = 'Goods + BeDo'; break;
			case "menu-contact" : newX = bWidth*2+gutter*2; newY = bHeight; $(this).addClass("menu-contact-on");document.title = 'Contact + BeDo'; break;
		}
								
		$("#screens-container").animate({top : -newY, left : -newX}, 1200, "easeInOutBack");
	}).address();
	
	$.address.externalChange(function(event) {
	  var eventVal = event.value;
	  eventVal = eventVal.substr(1)
	  if(eventVal==""){
	    eventVal = "home";
	  }
	  eventVal = "menu-"+eventVal;
	  	  
	  var newX = 0;
		var newY = 0;
		$("#menu a").removeClass();
		switch (eventVal) {
			case "menu-home" : $("#menu-home").addClass("menu-home-on"); document.title = 'Home + BeDo'; break;
			case "menu-about" : newX = bWidth+gutter; $("#menu-about").addClass("menu-about-on"); document.title = 'About + BeDo'; break;
			case "menu-work" : newX = bWidth*2+gutter*2; $("#menu-work").addClass("menu-work-on");document.title = 'Work + BeDo'; break;
			case "menu-talks" : newY = bHeight; $("#menu-talks").addClass("menu-talks-on");document.title = 'Talks + BeDo'; break;
			//case "menu-blog" : newY = bHeight; $("#menu-blog").addClass("menu-blog-on");document.title = 'Blog + BeDo'; break;
			case "menu-goods" : newX = bWidth+gutter; newY = bHeight; $("#menu-goods").addClass("menu-stuff-on");document.title = 'Goods + BeDo'; break;
			case "menu-contact" : newX = bWidth*2+gutter*2; newY = bHeight; $("#menu-contact").addClass("menu-contact-on");document.title = 'Contact + BeDo'; break;
		}
								
		$("#screens-container").animate({top : -newY, left : -newX}, 1200, "easeInOutBack");
	});

	$(".about-work-link").click(function() {
		$("#menu a").removeClass();
		$("#menu-work").addClass("menu-work-on");
		document.title = 'Work + BeDo';
    var newX = bWidth*2+gutter*2;
		$.fancybox.close();
		$("#screens-container").animate({top : 0, left : -newX}, 1200, "easeInOutBack");
	});
	
	$(".about-talks-link").click(function() {
		$("#menu a").removeClass();
		$("#menu-talks").addClass("menu-talks-on");
		document.title = 'Talks + BeDo';
		var newY = bHeight
		$.fancybox.close();
		$("#screens-container").animate({top : -newY, left : 0}, 1200, "easeInOutBack");
	});
	
	$("#logo-color").click(function() {
		$("#menu a").removeClass();
		$("#menu-home").addClass("menu-home-on");
		document.title = 'Home + BeDo';
		$("#screens-container").animate({top : 0, left : 0}, 1200, "easeInOutBack");
	});
	
	$("#learn-more").click(function(){
		$("#menu a").removeClass();
		var newX = bWidth+gutter; 
		$("#menu-about").addClass("menu-about-on");
		document.title = 'About + BeDo';
		$("#screens-container").animate({top : 0, left : -newX}, 1200, "easeInOutBack");
	});

	$("#see-more").click(function(){
		bgIndex++;
		if(bgIndex==3){
			$("#backgrounds").animate({left : 0}, 1200, "easeInOutBack");
			bgIndex = 0;
		} else {
			$("#backgrounds").animate({left : "-=970"}, 1200, "easeInOutBack");
		}
	});

	$("#paint-bucket").toggle(function(){
		$("#paint").animate({top : '+=10', opacity: 1}, 200);	
	}, function() {
		$("#paint").animate({top : '-=10', opacity: 0}, 200);	
	});
	
	$("#paint span").click(function(){
		//recolor
		recolor($(this));
	});

	$(".about-box").fancybox({'width'	: 560, 'height' : 520 , 'autoScale' : false, 'autoDimensions' : false, 'overlayOpacity' : 1, 'overlayColor': '', 'scrolling' : 'no' });
	$(".work-open").fancybox({'width'	: 610, 'height' : 560, 'autoScale' : false, 'autoDimensions' : false, 'overlayOpacity' : 1, 'overlayColor': '', 'scrolling' : 'no' });
	$(".talks-open").fancybox({'width'	: 610, 'height' : 640, 'autoScale' : false, 'autoDimensions' : false, 'overlayOpacity' : 1, 'overlayColor': '', 'scrolling' : 'no' });
	$(".background-link").fancybox({'width'	: 940, 'height' : 600, 'autoScale' : false, 'autoDimensions' : false, 'overlayOpacity' : 1, 'overlayColor': '', 'scrolling' : 'no' });
	$(".videoPop").fancybox({'overlayOpacity' : 1, 'overlayColor': '', 'scrolling' : 'no'});
	
	$(".about-box").click(function(){$("#fancybox-wrap").removeClass().addClass("about-overlay-box");})
	$(".work-open").click(function(){$("#fancybox-wrap").removeClass().addClass("work-overlay-box");})
	$(".background-link").click(function(){$("#fancybox-wrap").removeClass().addClass("goods-overlay-box");})

	$('a.email').each(function(){
			e = this.rel.replace('/','@');
			this.href = 'mailto:' + e;
			$(this).text(e);
	});
	
	/*
	$.ajax({
    type: "GET",
		dataType: "xml",
    url: "blog.php",
    success: parseXML
  });
	
	$("#blog").parent().css({"overflow-x": "hidden", "overflow-y": "scroll"});
	*/
	
	//remove blog section
	$("#blog").parent().remove();
	
	// hide iPhone URL bar
	setTimeout(function(){window.scrollTo(0, 1);}, 100);			
});

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
};
