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');
//				})
//			});
		});
	}
}

function gooiiSourceMenuBuild(el, path, id) {
	el.parent().siblings('p, ul').remove();
	el.parent().after('<p style="margin-top:0;">please wait...</p>');
	$.ajax({
		type: 'POST',
		data: 'menu='+path,
		success: function(html) {
			el.parent().next('p, ul').remove();
			if($(html).html() != '') {
				el.parent().after(html);
			} else {
				el.parent().after('<p style="margin-top:0;">no sources yet.</p>');
			}
			// reset iframe
			$('#' + id).attr({src: ''});
			// and remove link below
			$('.sourcePrev .upFrame, .sourcePrev .downFrame').html('');
		}
	});
}

function gooiiSourceDisplay(el, path, id, title) {
//	$.blockUI({
//		message: '<div class="popUp" style="height: 100%;"><iframe id="' + id + '" width="100%" height="100%" frameBorder="0"></iframe></div>',
//		css: {
//			border: 'none',
//			width: '50%',
//			left: '25%',
//			height: '50%',
//			top: '25%'
//		},
//		overlayCSS: {
//			opacity: 0.3
//		},
//		onBlock: function() {
//			$('#' + id).attr({src: path});
//		}
//	});
//	$('.blockOverlay').css({cursor: 'pointer'}).click(function() {
//		$.unblockUI();
//	})
	$('#' + id).attr({src: path});
	$('.sourcePrev .upFrame').html('<h2 class="section_title">'+title+'</h2><div><span>Posted in: '+$('body').find('h1').text()+' | Library: '+el.parent().parent().prev().text()+'</span></div><div class="hr"></div>');
	$('.sourcePrev .downFrame').html('<div class="hr"></div><div class="sourceNewWindow"><a href="'+path+'" target="source">Open in a new window</a> <span>|</span> <a href="'+path+'" target="source">Download Zip</a></div>')
}

$(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');
	$('.mainMenuHolder ul').each(function() {
		$(this).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'));

	// admin images
	$('#images').each(function() {
		function rebuild() {
			var arr = new Array();
			container.find('ul li:visible').each(function() {
				arr.push($(this).find('a:last').attr('rel'));
			})
			// arr.sort(function(a,b){return a - b});
			container.find('input:last').val(arr.join(','));
			container.find('ul li').find('a:last').click(function() {
				$(this).parent().fadeOut(function() {
					if(container.find('ul li').length > 1) {
						$(this).remove();
					}
					// create array
					rebuild();
				});
			});
		}
		var container = $(this);
		rebuild();
		container.find('select').next().click(function() {
			// check if item is selected from the list
			// check if item already exist on the list
			var id = $(this).prev().val();
			var text = $(this).prev('select').children('option:selected').text();
			if(id > 0 && container.find('ul li:visible a[rel='+id+']').length == 0) {
				container.find('ul li:first').clone().appendTo(container.find('ul'));
				container.find('ul li:last').show().find('a:first').text(text).next().attr({rel: id});
				container.find('ul li:hidden').remove();
				rebuild();
			}
		});
	});

	// prlx
	if($.browser.msie && $.browser.version == '6.0') {
	} else {
		$('.prlx').each(function() {
			$(this).fadeIn().children().parallax({
				mouseport: $('.header'),
				yparallax: false
			});
		});
	}
});
