$(document).ready(function() {
				
	$.getJSON('classes/marquee.php',{method:"dots"}, function(data) {
			$('#projectThumbs').html(data.content);
                        switchFeature(project);
                        projectMax = parseInt($("#projectThumbs > li > span > img:last").attr('alt'));
			/*$.getJSON('classes/marquee.php',{method:"marqueeImage",id:project}, function(init) {
				$("#projectThumbs > li > span > img[alt='"+project+"']").attr('src', 'images/interface/carouselSelected.jpg');
				$('#projectWrapper').append(init.content);
				$('#featureProject').css({'backgroundColor':'#'+init.color});
				$('#featureImage').css({'opacity':1});
				projectMax = parseInt($("#projectThumbs > li > span > img:last").attr('alt'));
				});*/
			$('#projectThumbs > li').click(function() {
				switchFeature(parseInt($(this).children('span').children('img').attr('alt')));
				});
			});
	
	$('#leftArrow').click(function() {
		if((project-1)<1) {
			// loop around to the end
			project = $("#projectThumbs > li > span > img:last").attr('alt');
			switchFeature(project);
			}
		else {
			switchFeature(project-1);
			}
		});
		
	$('#rightArrow').click(function() {
		if((project+1)>$("#projectThumbs > li > span > img:last").attr('alt')) {
			// loop around to the beginning
			project = 1;
			switchFeature(project);
			}
		else {
			switchFeature(project+1);
			}
		});
	$("#clickSpot").live('click',function() {
		$.address.value($(this).attr('rel'));
		return false;
		});
	$('span > a').live('click',function() {
		$.address.value($(this).attr('href'));
		return false;
		});
	
	$('p.video').live('click',function() {
		var title = $(this).attr('title');
		var video = $(this).attr('ref');
		video = video.split('|');
		var obj = new Object();
		obj.url = "/classes/video.php?host="+video[0]+"&id="+video[1]+"&width="+video[2]+"&height="+video[3];
		obj.type = "iframe";
		obj.title = title;
		obj.w = parseInt(video[2])+20;
		obj.h = parseInt(video[3])+20;
		openShadowbox(obj);
		return false;
		});
	
	/*
	$('p.image').live('click',function() {
		//var title = $(this).attr('title');
		var obj = new Object();
		obj.url = $("a",this).attr('href');
		obj.gallery = 'Portfolio';
		obj.type = "img";
		//obj.title = title;
		openImage(obj);
		return false;
		});
	*/
	
	//$('p.image a').shadowbox({gallery:'Portfolio'});
		
	/*///////////////////////////////
			  Deep Linking
	///////////////////////////////*/
	
	$.address.tracker(null);
	
	$.address.externalChange(function(event) {
		if(event.pathNames[0] == undefined) {
			$.address.value('/home/');
			//$.address.title('Hooky Interactive > '+event.pathNames[0]);
			$('#loading').fadeIn('fast');
			return false;
			}
		else {
			$.address.value(event.path);
			//$.address.title('Hooky Interactive > '+event.pathNames[0]);
			$('#contentContainer').css('top','-32px');
			$('#projects').hide();
			return false;
			}
		trackHtml($.address.path());
		});
		
	$.address.change(function(event) {
		if(event.pathNames[0] != undefined){
			if(event.pathNames[0] == 'home' || event.pathNames[0] == 'contact' || prevSection != event.pathNames[0]) {
				subNav = 0;
				}
			switchContent(event.pathNames[0],event.path,subNav);
			//$.address.title('Hooky Interactive > '+event.pathNames[0]);
			if(event.pathNames[0] == 'portfolio' || event.pathNames[0] == 'about' || event.pathNames[0] == 'culture' || event.pathNames[0] == 'news') {
				subNav = 1;
				}
			}
		prevSection = event.pathNames[0];
		trackHtml($.address.path());
		});
				
	});
			 
var project = 1;		// start @ project 1
var projectMax = '';
var marquee = 0;		// marquee starts as hidden (0)
var subNav = 0;			// need sub nav
var prevSection = '';	// previous section

/*function switchFeature(projectID) {
	project = projectID;
	clearDots();
	$("#projectThumbs > li > span > img[alt='"+project+"']").attr('src', 'images/interface/carouselSelected.jpg');
	$('#loading').fadeIn(function() {
		$('#featureImage').animate({opacity:0},250,function(){
			$.getJSON('classes/marquee.php',{method:"marqueeImage",id:project}, function(data) {
                            console.log(data);
				$('article#featureProject').css({'backgroundColor':'#'+data.color});
				$('#featureImage').remove();
				$('#projectWrapper').append(data.content);
				$('#featureImage > a > img').load(function() {
					$('#featureImage').css({'opacity':1});
					$('#loading').fadeOut();
					});
				});
			});
		});
	}*/
    
function switchFeature(projectID){
    console.log("hi");
    project = projectID;
    clearDots();
    $("#projectThumbs > li > span > img[alt='"+project+"']").attr('src', 'images/interface/carouselSelected.jpg');
    $.ajax({
        url : 'classes/marquee.php',
        data : {
            method:"marqueeImage",
            id:project
        },
        dataType: "json",
        beforeSend : function(){
            $('#loading').css("left",($(window).width()/2)-32);
            console.log(($(window).width()/2));
            $('#loading').fadeIn();
        },
        success: function(data){
            console.log("hi");
            //console.log(data);
            //console.log("url('"+ data.image+"') #" + data.color + " center no-repeat");
            $("#featureImage > a > img").attr("src",data.image);
            $("#featureImage > a > img").load(function(){
                $("#clickSpot").attr("rel",data.link);
                $('article#featureProject').css("background","url('"+ data.image+"') #" + data.color + " center no-repeat");
            $('#loading').fadeOut();
            })
        },
        complete: function(){
            
        }
    });
}
	
function clearDots() {
	$("#projectThumbs > li > span > img").attr('src', 'images/interface/carouselNotSelected.jpg');
	}

// set defalt easing equation
jQuery.easing.def = "easeInOutExpo";

function switchContent(section,url) {
        $('#contentLoader').css("left",($(window).width()/2)-16);
	Shadowbox.clearCache();
	$("html").animate({scrollTop:0}, 250);
	$("nav > ul > li > span").removeClass('current');
	$("span[title='"+section+"']").addClass('current');
	$(".sideNav > ul > li").removeClass('current');
	$(".sideNav > ul > li > span > a[href='"+$.address.path()+"']").parent().parent().addClass('current');
	var dataURL = 'classes/'+section+'.php?subNav='+subNav+'&url='+url;
	if (subNav == 0) {
		$('#content').animate({opacity:0},500,function(){
			$('#contentLoader').fadeIn('fast',function(){
				if($.address.pathNames() != 'home' && marquee == 1) {
					$('#contentContainer').animate({top:'-32px'},500,function(){
						marquee = 0;
						});
					$('#projects').effect('blind',{},500);
					}
				else if($.address.pathNames() == 'home' && marquee == 0) {
					$('#projects').show('blind',{},500,function(){
						marquee = 1;
						});
					$('#contentContainer').animate({top:'-56px'},500);
					}
				$.getJSON(dataURL,{}, function(data) {
					$.address.title('Hooky Interactive > '+data.title);
					$('#content').html(data.content);
					$('#loading').fadeOut('fast');
					$('#contentLoader').fadeOut('fast',function(){
						$('#content').animate({opacity:1},500);
						$('p.image a').shadowbox({gallery:'Portfolio'});
						});
					});
				});
			});
		}
	else {
		$('.rightDetail').animate({opacity:0},500,function(){
			$('#contentLoader').fadeIn('fast',function(){
				if($.address.pathNames() != 'home' && marquee == 1) {
					$('#contentContainer').animate({top:'-32px'},500,function(){
						marquee = 0;
						});
					$('#projects').effect('blind',{},500);
					}
				else if($.address.pathNames() == 'home' && marquee == 0) {
					$('#projects').show('blind',{},500,function(){
						marquee = 1;
						});
					$('#contentContainer').animate({top:'-56px'},500);
					}
				$.getJSON(dataURL,{}, function(data) {
					$.address.title('Hooky Interactive > '+data.title);
					$('.rightDetail').replaceWith(data.content);
					$('#loading').fadeOut('fast');
					$('#contentLoader').fadeOut('fast',function(){
						$('.rightDetail').animate({opacity:1},500);
						$('p.image a').shadowbox({gallery:'Portfolio'});
						});
					});
				});
			});
		}
	}

// autoplay marquee when on home section
// setInterval('if($.address.pathNames() == "home"){project++;if(project>projectMax){project = 1;}switchFeature(project)}',10000);

/*///////////////////////////
	   Loader Canvas
///////////////////////////*/

var rotation = 0;
var cog = new Image();

function loaderInit() {
	cog.src = 'images/interface/loader.png';
	setInterval(loaderCall,10);
	}
	
function loaderCall() {
	try {
		if(document.getElementById('loading')) {
			var ctx = document.getElementById('loading').getContext('2d');
			loader(ctx);
			}
		if(document.getElementById('contentLoader')) {
			var ctz = document.getElementById('contentLoader').getContext('2d');
			loader(ctz);
			}
		}
	catch(err) {
	
		}
	}

function loader(obj) {
	obj.globalCompositeOperation = 'destination-over';
	obj.save();
	obj.clearRect(0,0,74,74);
	obj.translate(37,37);
	rotation +=1;
	obj.rotate(rotation*Math.PI/64);
	obj.translate(-37,-37);
	obj.drawImage(cog,0,0);
	obj.restore();
	}
loaderInit();
/*
for shadowbox calling video
var tester2 = 	[
	{"content":"../images/books/large/Boom_Classics1.jpg","player":"img","title":"Boom Classics 1"},
	{"content":"../images/books/large/Boom_Classics2.jpg","player":"img","title":"Boom Classics 2"},
	{"content":"../images/books/large/Boom_Valentine.jpg","player":"img","title":"Boom Valentine"}
	];

http://www.yaelle.com/swf/test135.html

var galleryArray = [];
var resetShadowbox = function(){galleryArray = [];};
var add2shadowbox = function(item){galleryArray.push(item);};
var openGallery = function(id) {
	Shadowbox.open(galleryArray,shadowboxOptions,id);
};
var shadowboxOptions = {
	continuous:		false,
	counterType:	'skip',
	animate:		true
	}
*/
