// JavaScript Document

//Markisgood is root namespace
var Markisgood =
{
	ajaxDispatcherController:"http://www.markisgood.com/new/application/ajaxDispatcher.php",
	bg: null,
	
	onReady: function()
	{
		this.bg = new Markisgood.Background();
		this.mainNavHover();
		$("#mainbg").fadeTo(0,0.5);
		$("#contentcol1bg").fadeTo(0,0.5);
		$("#contentcol2bg").fadeTo(0,0.5);	
	},
	
 	extend: function(subClass,superClass)
	{
		var F = function(){};
		F.prototype = superClass.prototype
		subClass.prototype = new F();
		subClass.prototype.constructor = subClass;
		
		subClass.superclass = superClass.prototype;
		if(superClass.prototype.constructor == Object.prototype.constructor)
			superClass.prototype.constructor = superClass;
	}
}


//mainnav hover and background assignment
//used by all pages
Markisgood.mainNavHover = function()
{
	$("#navbuttons li a").hover(
		function()
		{
			if($(this).parent().attr("class") != "home")
			{
				var splittedHREF = $(this).attr("href").split("/");
				var HREFEnd = splittedHREF[splittedHREF.length - 1];
				if(page != HREFEnd)
				{
					$(this).css("background","#FFF");
					$(this).css("color","#C63");
					$(this).parent().css("list-style-image", "url(public/images/mainnavlistbg_over.png)");
				}
			}
			else
			{
				if(page != "home")
					$(this).css("background-image","url(public/images/new/homebutton_over.png)");
			}
		},
		function()
		{
			if($(this).parent().attr("class") != "home")
			{
				var splittedHREF = $(this).attr("href").split("/");
				var HREFEnd = splittedHREF[splittedHREF.length - 1];
				if(page != HREFEnd)
				{
					$(this).css("background","#C63");
					$(this).css("color","#FFF");
					$(this).parent().css("list-style-image", "url(public/images/mainnavlistbg.gif)");
				}
			}
			else
			{
				if(page != "home")
					$(this).css("background-image","url(public/images/new/homebutton.png)");
			}
		}						 
	);
	
	//set current mainnav background to current
	if(page == "home")
	{
		jQuery("#navbuttons .home a").css("background-image","url(public/images/new/homebutton_over.png)");
	}
	else if(page == "projects")
	{
		jQuery("#navbuttons li a[href$='projects']").css("background","#FFF");
		jQuery("#navbuttons li a[href$='projects']").css("color","#C63");
		jQuery("#navbuttons li a[href$='projects']").parent().css("list-style-image","url(http://www.markisgood.com/blog/wp-content/themes/inove/img/mainnavlistbg_over.png)");
	}
	else if(page == "contact")
	{
		jQuery("#navbuttons li a[href$='contact']").css("background","#FFF");
		jQuery("#navbuttons li a[href$='contact']").css("color","#C63");
		jQuery("#navbuttons li a[href$='contact']").parent().css("list-style-image","url(http://www.markisgood.com/blog/wp-content/themes/inove/img/mainnavlistbg_over.png)");	
	}
};

////background class
//used to transition and change background images
//used by: index and projects, loaded on all pages
Markisgood.Background = function()
{
	var _bgImages = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","6.jpg","7.jpg"];
	var _index =  0;
	
	_index = Math.floor(Math.random() * 6);
	$("body").css("background-image", "url(public/images/backgrounds/" + _bgImages[_index] + ")");
	
	this.change = function()
	{
		$("#backgroundback").css("background-image", $("#backgroundfront").css("background-image") );
		$("#backgroundfront").css("background-image", "inherit");
		$("#backgroundfront").fadeTo(0,0);
		
		if(_index <= _bgImages.length - 2)
			_index++;
		else
			_index = 0;
			
		$("#backgroundfront").css("background-image", "url(public/images/backgrounds/" + _bgImages[_index] + ")");
		
		$("#backgroundfront").fadeTo(1500,1.0, function()
		{
			$("body").css("background-image", "url(public/images/backgrounds/" + _bgImages[_index] + ")");
			$("#backgroundback").css("background-image", "inherit");
			$("#backgroundfront").css("background-image", "inherit");
		});
	};
};

//collapsible object
//superclass for all collapsible columns 
//using on: index and projects
Markisgood.Collapsible = function(column,columnBG)
{
	this.column = column;
	this.columnBG = columnBG;
}
Markisgood.Collapsible.prototype = 
{
	isAnimating: false,
	isCollapsed: true,
	isChangeBG: false,
	isShowLoading: false,
	column: null,
	columnBG: null,
	transitionTime: 500,
	collapsedHeightOffset: 0,
	expandedHeightOffset: 0,
	
	collapse: function(callBack,callBackArg)
	{
		$(this.column).css("visibility","hidden");
		this.isAnimating = true;
		
		var that = this;
		var isDone = false;
		function onAnimationComplete()
		{
			if(!isDone)
			{
				isDone = true;
				if(that.isShowLoading)
					that.onLoading();
							
				$(this.columnBG).css("visibility","hidden");
				that.isAnimating = false;
				that.isCollapsed = true;
						
				if(callBack)
					callBack(callBackArg);
			}
		}
		
		$(this.columnBG).animate({"height": this.collapsedHeightOffset},this.transitionTime, null, onAnimationComplete);
																											
	},

	expand: function(callBack)
	{
		if(this.isShowLoading)
			this.doneLoading();
		
		if(this.isChangeBG)
			Markisgood.bg.change();
		
		this.isAnimating = true;
		$(this.column).css("visibility","visible");
		$(this.column).fadeTo(0,0.0);
		$(this.columnBG).css("visibility","visible");
		
		var that = this;
		var isDone = false;
		function onAnimationComplete()
		{
			if(!isDone)
			{
				isDone = true;
				$(that.column).fadeTo(300,1.0);
				that.isCollapsed = false;
				that.isAnimating = false;
				
				if(callBack)
					callBack();
			}
		}
		
		$(this.columnBG).animate({"height": $(this.column).height() + this.expandedHeightOffset},this.transitionTime, null, onAnimationComplete);
	},
	
	onLoading: function()
	{
		$("#loadinggraphic").css("visibility","visible");
		$("#loadinggraphic").html("<h1>Loading</h1>");
	},
	
	doneLoading: function()
	{
		$("#loadinggraphic").css("visibility","hidden");
	},
}


//jQuery onReady call my on Ready
$(document).ready(function()
{	
	Markisgood.onReady();
});

