var counter_gallery = 0;
var total_gallery = 0;

$(document).ready(function(){

	/*Place any load code in here*/

	if ($("#wrapper-galleryone").length>0) {

		total_gallery = $(".thumbnail-gallery").length;

		$.ajaxSetup({
			dataType: "json"
		});

		//Sketches
		$.ajax({
			url: "http://api.flickr.com/services/feeds/photoset.gne?set=72157623440208148&nsid=47191451@N05&lang=en-us&format=json&jsoncallback=?",
			success: function(data) {
				loadphotos(data,"galleryone-sketches","one");
			},
			complete:function(data) {
				titleslide("galleryone-sketches","galleryone-elevations","one");
				counter_gallery = counter_gallery + 1;
			}
		});

		//Elevations
		$.ajax({
			url: "http://api.flickr.com/services/feeds/photoset.gne?set=72157623428066188&nsid=47191451@N05&lang=en-us&format=json&jsoncallback=?",
			success: function(data) {
				loadphotos(data,"galleryone-elevations","one");
			},
			complete:function(data) {
				titleslide("galleryone-elevations","galleryone-preliminary","one");
				counter_gallery = counter_gallery + 1;
			}
		});

		//Prelim
		$.ajax({
			url: "http://api.flickr.com/services/feeds/photoset.gne?set=72157623280772477&nsid=47191451@N05&lang=en-us&format=json&jsoncallback=?",
			success: function(data) {
				loadphotos(data,"galleryone-preliminary","one");
			},
			complete:function(data) {
				titleslide("galleryone-preliminary","galleryone-threedmodel","one");
				counter_gallery = counter_gallery + 1;
			}
		});

		//3d
		$.ajax({
			url: "http://api.flickr.com/services/feeds/photoset.gne?set=72157623405279948&nsid=47191451@N05&lang=en-us&format=json&jsoncallback=?",
			success: function(data) {
				loadphotos(data,"galleryone-threedmodel","one");
			},
			complete:function(data) {
				titleslide("galleryone-threedmodel","galleryone-videotour","one");
				counter_gallery = counter_gallery + 1;
			}
		});

		//Video Tour
		loadvideos("http://www.youtube.com/watch?v=8xH6mvDOYAU","tour","galleryone-videotour","one");
		counter_gallery = counter_gallery + 1;

		//Video Prefab Myths
		loadvideos("http://www.youtube.com/watch?v=-oxbWVlTFAY","prefab","galleryone-videoprefab","one");
		counter_gallery = counter_gallery + 1;

		//Video Architect
		loadvideos("http://www.youtube.com/watch?v=IYcSFheaqS0","architect","galleryone-videoarchitect","one");
		counter_gallery = counter_gallery + 1;

		//Video 2010 Dream Home
		loadvideos("http://www.youtube.com/watch?v=xwHMGeOriWE","dreamhome","galleryone-videodreamhome","one");
		counter_gallery = counter_gallery + 1;

		//Build
		$.ajax({
			url: "http://api.flickr.com/services/feeds/photoset.gne?set=72157623303254899&nsid=47191451@N05&lang=en-us&format=json&jsoncallback=?",
			success: function(data) {
				loadphotos(data,"gallerytwo-build","two");
			},
			complete:function(data) {
				titleslide("gallerytwo-build","gallerytwo-videobuild","two");
				counter_gallery = counter_gallery + 1;
			}
		});

		//Video The Build
		loadvideos("http://www.youtube.com/watch?v=TneT81vFsIg","build","gallerytwo-videobuild","two");
		counter_gallery = counter_gallery + 1;

		//Video The Builder
		loadvideos("http://www.youtube.com/watch?v=K8XJf2qPIU4","builder","gallerytwo-videobuilder","two");
		counter_gallery = counter_gallery + 1;

		//Applications
		$.ajax({
			url: "http://api.flickr.com/services/feeds/photoset.gne?set=72157623450349382&nsid=47191451@N05&lang=en-us&format=json&jsoncallback=?",
			success: function(data) {
				loadphotos(data,"gallerythree-apps","three");
			},
			complete:function(data) {
				titleslide("gallerythree-apps","gallerythree-country","three");
				counter_gallery = counter_gallery + 1;
			}
		});

		//Country Setting
		$.ajax({
			url: "http://api.flickr.com/services/feeds/photoset.gne?set=72157623453901518&nsid=47191451@N05&lang=en-us&format=json&jsoncallback=?",
			success: function(data) {
				loadphotos(data,"gallerythree-country","three");
			},
			complete:function(data) {
				titleslide("gallerythree-country","gallerythree-videocommercial","three");
				counter_gallery = counter_gallery + 1;
			}
		});

		//Video Commercial Applications
		loadvideos("http://www.youtube.com/watch?v=YlFXq86KVPU","commercial","gallerythree-videocommercial","three");
		counter_gallery = counter_gallery + 1;

		//Video Commercial Applications
		loadvideos("http://www.youtube.com/watch?v=oeDuIJL6nV8","applications","gallerythree-videoresidential","three");
		counter_gallery = counter_gallery + 1;

	}




	$("#nav-homepage li a").hover(function(){
		$("#navheader-homepage li").not($("#"+$(this).parent().attr("id").replace("nav","navheader"))).addClass("off");
	},function(){
		$("#navheader-homepage li").removeClass("off");
	});

	$("#nav-internal li a").hover(function(){
		$("#navheader-internal li#"+$(this).parent().attr("id").replace("nav","navheaderinternal")).addClass("on");
	},function(){
		$("#navheader-internal li").removeClass("on");
	});

	$("#subnav-principles li a").click(function(){
		changehomelayer($(this));
	});

	$(".descriptor-nav a.back").click(function(){
		changehomelayer($("#"+$(this).parent().parent().parent().attr("id").replace("wrapper-home-base","navprinciples")).prev().find("a"));
	});

	$(".descriptor-nav a.next").click(function(){
		changehomelayer($("#"+$(this).parent().parent().parent().attr("id").replace("wrapper-home-base","navprinciples")).next().find("a"));
	});

	$("#form-contact").validate();

});

function changehomelayer(obj) {
	$("#subnav-principles li a").removeClass("on");
	$(obj).addClass("on");
	$(".home-display").removeClass("layeron");
	$("#wrapper-home-base-"+$(obj).parent().attr("id").replace("navprinciples-","")).addClass("layeron");
}

function loadphotos(data,gallery,setnum) {
	$.each(data.items, function(i,item){
		descriptiontext = item.description.replace(/<\/?[^>]+(>|$)/g, "").replace("Mod256 posted a photo:  ","");
		itemtext = item.title;
		if (i==0) {
			thumblink = $("<a/>").attr("href", item.media.m.replace("_m","")).attr("class", "lightbox_" + setnum).attr("title",descriptiontext).attr("rel","lightbox[Design_"+setnum+"]");
			$(thumblink).html($("<img/>").attr("src", item.media.m).attr("class", "thumbpic").attr("alt",itemtext))
			$(thumblink).html($(thumblink).html() + "<img src='images/wdgt.zoom.png' width='16' height='16' alt='Zoom' class='zoom-gallery' />");
			$(thumblink).appendTo("#" + gallery + " .thumbnail-gallery");
		} else {
			newlink = $("<a/>").attr("href", item.media.m.replace("_m","")).attr("class", "lightbox_" + setnum).attr("title",descriptiontext).attr("rel","lightbox[Design_"+setnum+"]");
			$(newlink).html($("<img/>").attr("src", item.media.m).attr("alt",itemtext))
			$(newlink).prependTo("#" + gallery + " .imagecontainer");
		}

		//all galleries have been loaded
		show_galleries();
	});
}

function loadvideos(link,thumb,gallery,setnum) {
		thumblink = $("<a/>").attr("href", link).attr("class", "lightbox_" + setnum).attr("rel","lightbox[Design_"+setnum+"]");
		$(thumblink).html($("<img/>").attr("src", "images/thmb.video."+thumb+".jpg").attr("class", "thumbpic").attr("alt","YouTube"))
		$(thumblink).html($(thumblink).html() + "<img src='images/wdgt.zoom.png' width='16' height='16' alt='Zoom' class='zoom-gallery' />");
		$(thumblink).appendTo("#" + gallery + " .thumbnail-gallery");

		show_galleries();
}

function show_galleries() {
	if(counter_gallery == total_gallery-1) {
		counter_gallery = counter_gallery + 1;
		$("a[rel^='lightbox']").prettyPhoto({theme:'facebook',allowresize:false,opacity: 0.90});
		$(".loading").fadeOut();
	}
}

function titleslide(currentset,nextset,setnum) {
	slidelink = $("<a/>").attr("href", "#slideintro_" + currentset).attr("rel","lightbox[Design_"+setnum+"]");
	nextthumb = $("<img/>").attr("src", $("#" + nextset + " .thumbnail-gallery img").attr("src")).attr("class", "thumbpic nextsetthumb");
	slidehtml = $("<div/>").attr("class","slideintro").attr("id","slideintro_" + currentset).html("<p class='nextsetname'>" + $("#" + nextset + " .title-gallery").html() + "</p>")
	$(nextthumb).prependTo(slidehtml);
	$("<p class='endofset'>You have reached the end of this set</p><p>Continue to the next set:</p>").prependTo(slidehtml);
	$(slidelink).html(slidehtml);
	$(slidelink).appendTo("#" + currentset + " .imagecontainer");
}
