var shareThisObjects = {};
var addthis_pub="4corners";

// extensions
(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

//4c app
var currentpost = "none";
var loadInterval;

$(document).ready(function(){
	initModules();
	$.historyInit(pageload);
	
	var ssss = setInterval(function(){
		thisMovie("4clogo").nextobject();
	},4000);
	
	$('.fulltc a').each(function(el,i){
		$(el)[0].target = "_blank";
	})
})


function loadModules(tag,id) {
	var _data = "";
	if (tag!=null) _data+="ptag="+tag+"&"; 
	if (id!=null) {
		_data+="pid="+id;
		if ($("#module-"+id)[0]!=null) _data+="&so";
//		alert(_data);
	}
	
	$.ajax({
		type: "GET",
		url: "modules.php",
		data: _data,
		success: function(data){
			setTimeout(function(){
				$("#posts-wrapper").append(data);
				$(".module").not(".current").each(function(i,el){
					$(this).css({"display":"none"});
					setTimeout(function(){
						$(el).customFadeIn(1200);
					},i*300);
				})
				initModules();
			},1000);
		}
	});
}

function initModules() {
	
	$("#loading-msg").hide(500);
	
	$(".single .project-info-closed, .about-info-closed").hover(
		function(){
			$(this).css({"opacity":.8});
		},
		function(){
			$(this).css({"opacity":0});
		}
	)
	
	$(".single .project-info-closed, .about-info-closed").click(function(){
		$(this).siblings(".post-link").click();
	})
	
	$(".post-link, .cat").each(function(i,el){
		var _href = this.href.split("/");
	 	if (this.href.indexOf("#")==-1) {
			var hash = "#"+_href[_href.length-1];
			this.href = hash;
		}
	})
	
	// set onlick event for buttons
	$("a[@rel='history']").click(function(){
		
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		
		if ($.historyCurrentHash==hash) return;
		clearInterval(loadInterval);
			
		loadInterval = setTimeout(function(){
			$.historyLoad(hash);
		},300);
		return false;
	});                                           
}

function moduleInit(el) {
	$(".scrolling",el).jScrollPane();
	
	// jcarousel setup 
	var caroSize = $('.mycarousel img',el).length;
	function caroInit() {
		if ( $(".projectaction",el).html().indexOf("Close")!=-1 ) {
			$(".projectaction",el).click();
			$(".jcarousel-next",el).unbind("click",caroInit);
		}
	}
	
	function caroMove(dir) {
		var id = parseInt($(".gallery-cnt",el).html())+dir;
		
		if (id>1) $('.prevbtn',el).removeClass('disabled');
		else $('.prevbtn',el).addClass('disabled');
		
		if (id<caroSize) $('.nextbtn',el).removeClass('disabled');
		else $('.nextbtn',el).addClass('disabled');
		
		if (id>=1 && id<=caroSize) $(".gallery-cnt",el).html(id);
	}
	
    $('.mycarousel', el).jcarousel({
		initCallback:function(a,b){
			$(".jcarousel-next",el).bind("click",caroInit);
		},
		visible:1
	});
	
	$('.jcarousel-next',el).click(function(){
		caroMove(1);
	})
	
	$('.jcarousel-prev',el).click(function(){
		caroMove(-1);
	})
	
	$('.prevbtn',el).click(function(){
		$('.jcarousel-prev',el).click();
		return false;
	})
	
	$('.nextbtn',el).click(function(){
		$('.jcarousel-next',el).click();
		return false;
	})
	
	
	$(".projectaction",el).click(function(){
		$(this).nextAll(".project-info").slideToggle("fast");		
		if ($(this).is(":contains('Close')")) {
			var newt =  "Open Description";
			$('.posttitle-small',el).show();
			$('.cattitle',el).css({marginBottom:"5px"});
			
		}else{
			var newt =  "Close Description";
			$('.posttitle-small',el).hide();
			$('.cattitle',el).css({marginBottom:"20px"});
		}
		
		$(this).text(newt);
		return false;
	})
}

function openModule(elid) {
	
	var el = $('#module-'+elid);
	$(".content",el).customFadeOut(600);
	
	$(".content-expand-double",el).css({display:"block","opacity":0});
	$(".content-expand-triple",el).css({display:"block","opacity":0});
	
	moduleInit(el);
	
	setTimeout(function() {
		if ($(".content-expand-double",el).get(0)!=null) {
			$(el).addClass("module-expand-double");
			$(".content-expand-double",el).animate({"opacity":1},600,function(){
				if(jQuery.browser.msie)
					$(this).get(0).style.removeAttribute('filter');
			});						
		}
		else if ($(".content-expand-triple",el).get(0)!=null) {
			$(el).addClass("module-expand-triple");
			$(".content-expand-triple",el).animate({"opacity":1},600,function(){
				if(jQuery.browser.msie)
					$(this).get(0).style.removeAttribute('filter');
			});						
		}	
	},600)	
	$(el).addClass("current");
}

function pageload(hash) {
	if(hash!="") {
		// restore ajax loaded state
		var tag, id, clicked;
		
		if (hash.indexOf("tag:")!=-1) {
			var _s = hash.split("tag:");
			tag = _s[1];
		}
		if (hash.indexOf("post:")!=-1) {
			var _s = hash.split("post:");
			id = _s[1];
		}		
		
		if (id==currentpost) return;
		if (id!=null) {
			clicked = $('#module-'+id).get(0);
			currentpost = id;
		}else{
			currentpost = "none";
		}		
		
		$("#loading-msg").show(500);
		loadModules(tag,id);
		
		//reflect state to relevant links
		$(".selected").removeClass("selected");
		if (tag==null) {
			/* TODO finish it of
			if (id!=null) {
				var tags = [];
				//find relevant tags for selected post
				var _tag = $("#module-"+id+" .content")[0].id.replace("tag_","");
				tag.push(_tag);
				
			}
			*/
		}else{
			$(".tag-"+tag).addClass("selected");
		}	
		
		setTimeout(function(){
			thisMovie("4clogo").nextobject();
		},1000);
	
		//remove current
		$('.module').each(function(i,el){
			if (el!=clicked) {
				$(el).css({"opacity":1});	
				
				setTimeout(function(){
					$(el).customFadeOut(600,function(){
						$(el).remove();
					});
				},50);
			}else{
				$(".content",el).customFadeOut(600);
				setTimeout(function(){
					$(".content-expand-double",el).css({display:"block","opacity":0});
					$(".content-expand-triple",el).css({display:"block","opacity":0});
					
					moduleInit(el);
					
					setTimeout(function() {
						if ($(".content-expand-double",el).get(0)!=null) {
							$(el).addClass("module-expand-double");
							$(".content-expand-double",el).animate({"opacity":1},600,function(){
								if(jQuery.browser.msie)
									$(this).get(0).style.removeAttribute('filter');
							});						
						}
						else if ($(".content-expand-triple",el).get(0)!=null) {
							$(el).addClass("module-expand-triple");
							$(".content-expand-triple",el).animate({"opacity":1},600,function(){
								if(jQuery.browser.msie)
									$(this).get(0).style.removeAttribute('filter');
							});						
						}	
					},600)	
					$(el).addClass("current");
				},i*100);
			}
		})
		
	}
}

String.prototype.capitalize = function(){ //v1.0
	return this.replace(/\w+/g, function(a){
		return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
	});
};

function thisMovie(movieName) {
	return document.getElementById(movieName);
}

//flash logo
var flashvars = {}
var params = {
	menu: "false",
	wmode: "transparent"
}
var attrs = {
	id: "4clogo",
	name: "4clogo"
}
swfobject.embedSWF("logo.swf", "logo-anim", "230", "230", "8.0.0", "expressInstall.swf",flashvars,params,attrs);