function ajaxPost(root, string, target) {
	if(target) {
		$(target).wrapInner('<div class="opacity"></div>');
		$('.opacity').css({opacity: 0.2});
	}
	$.ajax({
		type: 'POST',
		url: root+'index.php',
		data: string+'&ajax=1',
		success: function(html) {
			if(target) {
				$(target).html(html);
				// update right column
				$.ajax({
					type: 'POST',
					url: root+'index.php',
					data: 'rightColumn=1&ajax=1',
					success: function(html) {
						$('#contentRight').html(html);
						// view article
						$(target+', #contentRight').find('a').each(function() {
							if($(this).attr('id') > 0) {
								var id = $(this).attr('id');
								var category = $(this).attr('rel');
								$(this).attr({href: '#/'+category+'/'+id});
								$(this).unbind().bind('click', function() {

									// checking if client execute
									var id_name;
									if(category == 'client') {
										category = 'portfolio';
										id_name = 'ClientID';
									} else {
										id_name = 'PostID';
									}

									ajaxPost(category+'/', id_name+'='+id, '#contentLeft');
								})
							}
						});
						// pagination
						$('.paginate a').each(function() {
							var string = $(this).attr('rel').substr(1);
							var new_string = string.split('=');
							$(this).attr({href: '#/'+root+''+new_string[0]+'/'+new_string[1]});
							$(this).unbind().bind('click', function() {
								//alert('root: '+root+', string: '+string)
								ajaxPost(root, string, '#contentLeft');
							});
						});
					}
				});
			}
		}
	});
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function blendimage(divid, imageid, imagefile, millisec, loader) {
		var myImage = new Image(); 		
		myImage.src = "../_phpThumb/phpThumb.php?src=" + imagefile + "&w=550";
	// Split the string into part [0] and part [1]
	temp=navigator.appVersion.split('MSIE');
	// Parse the string for the "6" in 6.0
	ieVer=parseInt(temp[1]);
	// Is it greater than 6?
	if (ieVer==6) {
		document.getElementById(loader).style.display = "none";
		document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
		document.getElementById(divid).style.backgroundRepeat = "no-repeat";
		document.getElementById(imageid).src = "../_phpThumb/phpThumb.php?src=" + imagefile + "&w=550";
	} else {
		if (myImage.complete) {
			var speed = Math.round(millisec / 100);
			var timer = 0;
	
			document.getElementById(loader).style.display = "none";
	
			//set the current image as background
			document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
			document.getElementById(divid).style.backgroundRepeat = "no-repeat";
			//make image transparent
			changeOpac(0, imageid);
			
			//make new image
			document.getElementById(imageid).src = "../_phpThumb/phpThumb.php?src=" + imagefile + "&w=550";
		
			//fade in image
			for(i = 0; i <= 100; i++) {
				setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
				timer++;
			}
		}
		else if (!myImage.complete) {
			var w = document.getElementById(imageid).width;
			var h = document.getElementById(imageid).height;
			t = (h-65)/2;
			l = (w-65)/2;
			document.getElementById(loader).style.display = "block";
			document.getElementById(loader).style.left = l;
			document.getElementById(loader).style.top = t;
			setTimeout("blendimage('" + divid + "','" + imageid + "','" + imagefile + "','" + millisec + "','" + loader + "')",1000);
		}
	}
}

function splitHash(url) {
	var string = location.hash.slice(2).split("/");
	return string;
}

function pageLoad(hash) {
	if(hash) {
		var value = location.hash.slice(2).split("/");
		ajaxPost(value[0]+'/', 'PostID='+value[1], '#contentLeft');
		//ajaxPost(value[0]+'/', 'rightColumn=1', '#contentRight');
		//animateMenu(value[0]+'/');
	}
	return false;
}

function animateMenu(rel) {
	var markerMargins = 35;
	var selectedItem = 'left:'+($('.mainMenuHolder ul li.selected a').position().left)+'px;width:'+($('.mainMenuHolder ul li.selected a').width()+markerMargins)+'px';
	if(rel) {
		if(rel == '/') {
			$('.mainMenuHolder ul li a:first').parent('li').addClass('selected').siblings().removeClass('selected');
			var width = $('.mainMenuHolder ul li a:first').width()+markerMargins;
			var position = $('.mainMenuHolder ul li a:first').position().left;
		} else {
			$('.mainMenuHolder ul li a[rel='+rel+']').parent('li').addClass('selected').siblings().removeClass('selected');
			var width = $('.mainMenuHolder ul li a[rel='+rel+']').width()+markerMargins;
			var position = $('.mainMenuHolder ul li a[rel='+rel+']').position().left;
		}
		$('.marker').animate({width: width, left: position+'px'}, 200, 'easeInOutQuad');
	} else {
		$('.mainMenuHolder ul').before('<div class="marker" style="'+selectedItem+'"><div class="l"></div><div class="r"></div><div class="m"></div></div>');
		$('.mainMenuHolder ul li a').each(function() {
			var url = $(this).attr('href');
//			$(this).attr({href: '#/'+url, rel: url});
//			$(this).click(function() {
//				$(this).parent().addClass('selected').siblings().removeClass('selected');
//				$('.marker').animate({width: ($(this).width()+markerMargins), left: $(this).position().left+'px'}, 200, 'easeInOutQuad', function() {
//					// old window.location = url;
//					//ajaxPost(url, '', '#contentLeft');
//					//ajaxPost(url, 'rightColumn=1', '#contentRight');
//				})
//			});
		});
	}
}

$(document).ready(function() {
	$('form.search input.text').click(function() {
		$(this).attr({value: ''});
		$(this).addClass('hover');
	});
	// homepage ajax
	//var url = $('.mainMenuHolder ul li a:first').attr('rel');
	//ajaxPost(url, '', '#contentLeft');
	//ajaxPost(url, 'rightColumn=1', '#contentRight');
	// add History and Back button
	pageLoad();
	$.historyInit(pageLoad);
	$("a[rel=history]").live('click', function() {
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		$.historyLoad(hash);
	});
	$(".mainMenuHolder ul").kMenu({
		bind: 'mouseover',
		revert: true,
		indicatorClass: 'marker',
		height: 80,
		easing: 'easeInOutQuad',
		callBefore: function() {
			$('.marker').html('<div class="l"></div><div class="r"></div><div class="m"></div>');
		}
	});
	// replace keywords
	$('.footer').after($('#kw'));
});