
function site(arr){
	this.currentObjects = arr;
	//
	this.collections = null;
	this.collectionImageWidth = 30;
	//
	var thisObj = this;
	//this.loadObjects(arr);
	var inAct = false;
	$('#nav').hover(
		
			function () {
				if(!inAct){
					$(this).animate({ 
						bottom: '0px'
					}, 300, "swing", function(){
						inAct = false;
					});
					inAct = true;
				}
			},
			function () {
				if(!inAct){
					$(this).animate({ 
						bottom: '-200px'
					}, 300, "swing", function(){
						inAct = false;
					});
					inAct = true;
				}
			}
			
	);
	// INIT NAVIGATION
	$.getJSON("portfolio/Portfolio.php?q=getCollections", function(data){
		thisObj.createNavigation(data);
		thisObj.openFirstCollection();
	});
}
site.prototype.openFirstCollection = function(){
	var arr = new Array();
	this.renderFirst(0, 0, arr);
	for(var i = 0; i<arr.length; i++){
		if(i < arr.length-1){
			this.toggle(arr[i]);
		}else{
			this.openCollection(arr[i]);
		}
	}
	
}
/* NAVIGATION */

site.prototype.createNavigation = function(data){
	//alert(data)
	this.collections = data.collections;
	$('#navigation').html(this.render(0, 0));
	$('.level1').append("<div class='spacing10'></div>");
}
site.prototype.sort = function(id){
    var retArr = [];
    for(var i = 0; i < this.collections.length; i++){
        if(this.collections[i].collectionParent == id){
        	//log(this.collections[i]);
    		retArr.push(this.collections[i]);
    	}
    }
    return retArr;
}
site.prototype.render = function(id, level){
	var ret = "";
    var objects = this.sort(id);
    if (objects.length>0){
    	for(var i in objects){
    		child = this.render(objects[i].collectionId, level + 1); 
    		if(child == ""){
    			ret += this.createLeaf(objects[i], level);
    		}else{
    			ret += this.createItem(objects[i], level, child);
    		}	
    	}
    }
    return ret;
}

site.prototype.renderFirst = function(id, level, arr){
    var objects = this.sort(id);
    if (objects.length>0){
    	arr.push(objects[0].collectionId);
    	this.renderFirst(objects[0].collectionId, level + 1, arr); 
    }
}


function returnHeadline(str, id, level){
	if(id){
		str = '<a class="collectionToggle" onclick="Hitsville.toggle(\''+id+'\')" onfocus="blur()">'+str+'</a>';
	}
	return "<h1 class='h"+level+" collection'>"+str+"</h1>";
}
function returnLeaf(str, id, level){
	if(id){
		str = '<a class="collectionLeaf" onclick="Hitsville.openCollection(\''+id+'\')" onfocus="blur()">'+str+'</a>';
	}
	return "<h1 class='h"+level+" leaf'>"+str+"</h1>";
}
site.prototype.toggle = function(id){
	if($('#c_'+id).is(":hidden")){
		$('#c_'+id).slideDown(200);
	}else{
		$('#c_'+id).slideUp(200);
	}
}
site.prototype.createLeaf = function(o, level){
	var stopNesting ="";
	var ret = '<li>';
		ret += returnLeaf(o.collectionMetaHeadline, o.collectionId, level);

			ret += '<div class="spacing0"></div>';
		
		ret +=	"</li>";
	return ret;
}
site.prototype.createItem = function(o, level, child){
	//
	if(!level){
		var ret = '<li class="firstNav">';
	}else{
		var ret = '<li>';
	}
	
		ret += returnHeadline(o.collectionMetaHeadline, o.collectionId, level);
		ret += "<ul id='c_"+o.collectionId+"' class='level level"+level+"'>";
		ret += child;
		ret += "</ul>";

		ret += '<div class="spacing0"></div>';
		
		ret += '</li>';
    return ret;
}






/******/


site.prototype.openCollection = function(id){
	var thisObj = this;
	this.setCollectionDescription(id);
	$.getJSON("portfolio/Portfolio.php?q=getCollectionImages&collectionId="+id, function(data){
		thisObj.loadObjects(data.images);
		
	});
}

site.prototype.createImage = function(id){
	var thisObj = this;
	var nextId = id+1;
	if(id < this.currentObjects.length){
	//	$('#flikar').append();
		
		
		//
		var tmb = "portfolio/images/image.php/"+this.currentObjects[id].imageSrc+"?height=400&image=/portfolio/images/imagesrc/"+this.currentObjects[id].imageSrc;
	//	$('#ut').append("&lt;img src='"+ tmb + "'><br>");
		var img = new Image();
		img.path = $('#f_'+id);
		img.idNo = id;
	  	$(img).load(function () {
	  	   $('#f_'+this.idNo + ' > #holder').html(this);
			//alert("NEXT D = "+nextId)
			thisObj.createImage(nextId);
			$(this.path).fadeIn();

	  	}).attr('src', tmb);
			
		$('#f_'+id).click(function(){
			var did = this.id.replace('f_', "");
			thisObj.animateObj(did);
		});
		if(id == 0){
			$('#f_'+id).width(200);
		}else if(id == 1){
			$('#f_'+id).width(100);
		}else if(id == 2){
			$('#f_'+id).width(300);
		}else{
			var split = (380-this.currentObjects.length)/(this.currentObjects.length-3);
			//console.log(split + " " +this.currentObjects.length)
			this.collectionImageWidth = split;
			$('#f_'+id).width(split);
		}
		$('#f_'+id+' > #holder').animate({opacity: .2}, 0);
		$('#f_'+id).mouseover(function(){
			if(!$(this).hasClass('marked')){
				$(this).addClass('ovr');
			}
		}).mouseout(function(){
			if(!$(this).hasClass('marked')){
				$(this).removeClass('ovr');
			}
		});
	}
		
}
site.prototype.loadObjects = function(data){
	var thisObj = this;
	thisObj.currentObjects = data;
	
	$('#flikar').empty();
	for(var i = 0; i < data.length; i++){
		var newLi = "<li id='f_"+i+"' class='imgs'><div id='holder'></div></li>";
	
		$('#flikar').append(newLi);
			$('#f_'+i).hide();
	}
	var hej = "";
	this.createImage(0)
	//alert(hej)
}
site.prototype.setSelectedCollection = function(id){
	this.parentCollection = null;
	for(var i = 0; i<this.collections.length;i++){
		if(this.collections[i].collectionId == id){
			this.selectedCollection = this.collections[i];
			break;
		}
	}
	for(var j = 0; j < this.collections.length; j++){
		if(this.collections[j].collectionId == this.selectedCollection.collectionParent){
			this.parentCollection = this.collections[j];
		}
	}
	return false;
}
site.prototype.setCollectionDescription = function(id){
	var thisObj = this;
	thisObj.setSelectedCollection(id);
	$('#collectionDescription').fadeOut(function(){
		$('#collectionDescription > h1').text(thisObj.parentCollection.collectionMetaHeadline);
		$('#collectionDescription > h2').text(thisObj.selectedCollection.collectionMetaHeadline);
	}).fadeIn();
	//alert(thisObj.selectedCollection.collectionMetaDescription);
	$('#image_description').fadeOut();
}
site.prototype.setImageDescription = function(id){
	var thisObj = this;
	$('#imageDescription').fadeOut(function(){
		$('#imageDescription > h2').text(thisObj.currentObjects[id].imageMetaContent);
	}).fadeIn();
	console.log("TEXT: " + thisObj.currentObjects[id].imageMetaContent)
}



site.prototype.animateObj = function(id){
	var newSmallWidth = $('#f_'+id+' > #holder > img').width();
	newSmallWidth += this.currentObjects.length;
	newSmallWidth = (980-newSmallWidth)/(this.currentObjects.length-1);
	var wArr = new Array();
	for(var i = 0; i < this.currentObjects.length; i++){
		if(i != id){
			wArr.push(newSmallWidth)
			if($('#f_'+i).hasClass('marked')){
				$('#f_'+i+' > #holder').fadeTo(200, .2);
				$('#f_'+i).removeClass('marked');
			}
		}else{
			$('#f_'+i+' > #holder').fadeTo(200, 1);
			$('#f_'+i).addClass('marked');
			$('#f_'+i).removeClass('ovr');
			var w = $('#f_'+i+' > #holder > img').width();
			wArr.push(w)
		}
	} 
	for(var i = 0; i < this.currentObjects.length; i++){
		$('#f_'+i).width(wArr[i]+"px")
	}
	this.setImageDescription(id);
}
site.prototype.returnAElement = function(id){
	return "<a id='c_"+id+"'>"+id+"</a>";
}

function toggleFlikar(id){

}

