$(document).ready(function(){
	$('#nav>ul>li>a').corner('round 8px', "#fff");
	$('#gallery-chooser>ul>li>a').corner('round 8px', "#fff");
	//$('#cshadow').corner('br 8px', "#fff");
	//$('#nav').corner('br 10px',"#e0d8ba");
	$.tabs('content');
	
 	setOpacity("#shadow",0.6);
 	setOpacity("#onNav",0.7);
	// setOpacity(".overlay_bg", 0.2);
 	
	undisparate();
 	setMainAreaHeight();
  	setVisuals();
  	setupGallery();
  	prepareTB();
  	prepareJunkForm();
	positionLogo();
});



function resizeRefresh(){
	setMainAreaHeight();
	positionLogo();
}

function positionLogo(){
	var welcomePos = $("#topMenu").offset().left;
	// alert(welcomePos);
	$("#logo").css({position: "absolute", left: welcomePos + 50 + "px", top : "75px"});
} 

function reinit(){
	$.tabs('content');
	setMainAreaHeight();
  	setVisuals();
}

$.tabs = function(containerId, start) {
    var ON_CLASS = 'on';
    var id = '#' + containerId;
    var i = (typeof start == "number") ? start - 1 : 0;
    
    $('#content>div:eq(' + i + ')').css({display:"block"});
    $('#nav>ul>li:nth-child(' + (i+1) + ')').addClass(ON_CLASS);
    
    $('#nav>ul>li>a').click(function() {
        if (!$(this.parentNode).is('.' + ON_CLASS)) {
            var re = /([_\-\w]+$)/i;
            var target = $('#' + re.exec(this.href)[1]);
            if (target.size() > 0) {
                $('#content>div:visible').css({display:"none"});
                target.css({display:"block"});
                $('#nav>ul>li').removeClass(ON_CLASS);
                $(this.parentNode).addClass(ON_CLASS);
                this.blur();
				$('#nav, #content').css({height : "auto"});
				undisparate();
            } else {
                alert('There is no such container.');
            }
        }
        return false;
    });
};

function undisparate(){
	var navH = $('#cshadow').height();
	var contentH = $("#content").height();
	
	if( navH > contentH){
		$("#content").css({height: navH+"px"});
	}
	else if(contentH > navH){
		$("#nav").css({height: contentH+"px"});
	}
	
	// window.setTimeout("undisparate()", 1);
}

setMainAreaHeight = function(){
	var winH= $(window).height();
	var mainH= winH - ( $('#topMenu').height() +  $('#shadow').height() +  $('#banner').height() +$('#logos').height() + 30);
	var navH = $('#cshadow').height();
	var contentH = $("#content").height();
	
	// $("#footer").appendTo(".shadowContent");
	
	// $('#package').css({height: mainH, overflow: "auto"});
	
	$('#footercenter').css({height: $('#logos').height(), overflow: "auto"});
	
	// $('body').css({overflow:"hidden"});
}

function setOpacity(target,percentage) {
	$(target).css({opacity:percentage});
}

function doTweet(){
	$("#motto").html("").tweet({
	          join_text: "auto",
				// intro_text: "(via twitter)",
	          username: "yayan_n",
	          avatar_size: 48,
	          count: 1,
	          auto_join_text_default: "I said,", 
	          auto_join_text_ed: "I",
	          auto_join_text_ing: "I was",
	          auto_join_text_reply: "I replied",
	          auto_join_text_url: "I was checking out",
	          loading_text: "loading tweets..."
	 }).fadeIn("slow");
}

function waitFade(){
	$('#motto').fadeOut("slow");
	window.setTimeout("writeQuote()", 500);
}
 
 function writeQuote(){
	// $('#motto').;
	var ran = Math.random()*2;
	// alert(ran);
	if(ran > 1.3){
			$('#motto').html(quote()).fadeIn("slow");
	}
		else{
		doTweet();
	}
	
 	window.setTimeout("waitFade()", 30000);
 	putLogo();
}

function putLogo(){
	// $("#logo").css({'background-image': "url("+ getImage() +")"});
	$('#logo').hide();
	$("#logo").html("<img src='"+ getImage() +"' alt='A random image' height='160px'/>").fadeIn("slow");
	//$("#logo").css({'background-image': "url(http://localhost/yann/symphony-2.0.1/workspace/images/puppy.png)"});
}

function setVisuals(){
 	$("html>body").attr({'onResize': 'resizeRefresh()'});
 	writeQuote();
 }

function prepareTB(){
	$("a.thickbox").click(function(){ 
		var t = this.title || this.name || this.href || null; 
		tb_show(t,this.href); 
		this.blur();
		return false; 
	}); 
}

function setupGallery(){
	var ON_CLASS = 'on';
	
	$("#gallery-chooser>ul>li>a").click(function(){ 
		$('#gallery-chooser>ul>li').removeClass(ON_CLASS);
        $(this.parentNode).addClass(ON_CLASS);
                
		var t = this.title; 
		retrievePictures(t);
		this.blur();
		return false; 
	});
}

function prepareJunkForm(){
	$('#addjunk').find('form').submit(function(){
    	$.get('php/functions.php',($(this).find('input')).serialize(), 
    			function(data) { 
      				pursue(data);
    			}
    	);
    	$(this).html("<p>Thanks for your contribution.</p><br/><a href='void'>Add another link</a>");
    	$(this).find('a').click(function(){
    		$(this.parentNode).append('<form id="junkform" action="">');
			$(this.parentNode).append('<p>Link your junk. Please keep it clean!</p>');
			$(this.parentNode).append('<p>Your name:<br/><input type="text" name="name" id="name"/></p>');
			$(this.parentNode).append('<p>Link:<br/><input type="text" name="linked" id="linked" size="60"/></p>');
			$(this.parentNode).append('<p>A short description of the junk:<br/><input type="text" name="desc" id="desc" size="60"/></p>');
			$(this.parentNode).append('<input type="hidden" name="type" id="type" value="addjunk"/>');
			$(this.parentNode).append('<input type="submit" value="Send!"/>');
			$(this.parentNode).append('<input type="reset" value="Clear"/>');
			$(this.parentNode).append('</form>');
			return false;
    	});
    	
    	return false; 
  	});
} 

///////////////////////////  round corners jquery plugin /////////////////////////////////////////////////////////////////
$.fn.corner = function(o, col)
{
	o = o || "";
	var width = parseInt((o.match(/(\d+)px/)||[])[1]) || 10;
	var fx = (o.match(/round|bevel|fold|notch/)||["round"])[0];
	var opts = {
		TL:		/top|tl/i.test(o), 		TR:		/top|tr/i.test(o),
		BL:		/bottom|bl/i.test(o),	BR:		/bottom|br/i.test(o)//,
	};
	if ( !opts.TL && !opts.TR && !opts.BL && !opts.BR )
		opts = { TL:1, TR:1, BL:1, BR:1 };
	var strip = document.createElement("div");
	strip.style.overflow = "hidden";
	strip.style.height = "1px";
	strip.style.backgroundColor = "transparent";
	strip.style.borderStyle = "solid";
	return this.each(function(){
		var pad = {
			T: parseInt($.css(this,"paddingTop"))||0,
			R: parseInt($.css(this,"paddingRight"))||0,
			B: parseInt($.css(this,"paddingBottom"))||0,
			L: parseInt($.css(this,"paddingLeft"))||0
		};
		strip.style.borderColor = col;
		if ( opts.TL || opts.TR ) {
			strip.style.borderStyle = "none "+(opts.TR?"solid":"none")+" none "+(opts.TL?"solid":"none");
			var t=document.createElement("div");
			t.style.margin = "-"+pad.T+"px -"+pad.R+"px "+(pad.T-width)+"px -"+pad.L+"px";
			t.style.backgroundColor = "transparent";
			for ( var i=0; i < width; i++ ) {
				var w = fx=="round" ? Math.round(width*(1-Math.cos(Math.asin(i/width)))) : i+1;
				var e = strip.cloneNode(false);
				e.style.borderWidth = "0 "+(opts.TR?w:0)+"px 0 "+(opts.TL?w:0)+"px";
				t.insertBefore(e, t.firstChild);
			}
			this.insertBefore(t, this.firstChild);
		}
		if ( opts.BL || opts.BR ) {
			strip.style.borderStyle = "none "+(opts.BR?"solid":"none")+" none "+(opts.BL?"solid":"none");
			var b=document.createElement("div");
			b.style.margin = (pad.B-width)+"px -"+pad.R+"px -"+pad.B+"px -"+pad.L+"px";
			b.style.backgroundColor = "transparent";
			for ( var i=0; i < width; i++ ) {
				var w = fx=="round" ? Math.round(width*(1-Math.cos(Math.asin(i/width)))) : i+1;
				var e = strip.cloneNode(false);
				e.style.borderWidth = "0 "+(opts.BR?w:0)+"px 0 "+(opts.BL?w:0)+"px";
				b.appendChild(e);
			}
			this.appendChild(b);
		}
	});
};




