

var ph = {

	currentPos:0,
	
	$links:null,
	
	startDragX:0,
	
	rootNote:'',
	subNote:'',
	
	currentPage:'',
	
	$currentSub:null,
	request:null,
	
	init:function () {
		// bind jquery address
		$.address.change(ph.onChange);
		
		ph.$links = $('#menu > li > a');
		$('#content-slider').scroll(ph.onScroll);
		
		
		/** supmenu **/
		$('#menu').find('li').hover(
		function (e) {
			$(this)
				.children('ul')
				.css({'display':'block', 'opacity':'0'})
				.stop()
				.animate({opacity:1},500);
			}, 
		function (e) {
			$(this).children('ul')
				.stop()
				.animate({opacity:0},500, function () {
					$(this).css('display','none');
				});
			
			});
	},
	
	
	
	onChange: function (obj) {
		
		var links = obj.path.split('/');
		
		var page = links[1];
		
		if (page == "") page = "home";
		$link = $('#menu').find('a[href="#'+page+'"]');
		
		ph.rootNote = page;
		
		if (typeof(links[2]) != "undefined"){
			page = links[1]+'_'+links[2];
			ph.subNote = links[2];
		}else{
			ph.closeSub();	
		}
			
		/** main menu found* **/
		if ($link.length == 1) {
			
			triggers.close(ph.currentPage);
			
			ph.currentPage = page;
			
			
			$('.m-activ')
				.removeClass('m-activ');
				
			$link.addClass('m-activ');
			
			// find pos
			var pos = ph.$links.index($link);
			ph.currentPos = pos;
				
			
			$('#content-slider')
				.stop()
				.animate({scrollLeft:(WIN_WIDTH * pos)},700);
			
			if (typeof(links[2]) != "undefined") {
				ph.loadSub(ph.subNote, obj.parameters);
				
			}
			
			triggers.open(ph.currentPage);
		}
		
	},
	
	
	onResize: function () {
		$('#content-slider')
			.stop()
			.scrollLeft(WIN_WIDTH * ph.currentPos)
		
	},
	
	
	next: function () {
		if ((ph.currentPos + 1) < ph.$links.length){
			ph.currentPos ++;
			window.location.hash = ph.$links.eq(ph.currentPos).attr('href');
		}
	},
	
	
	prev: function () {
		if ((ph.currentPos - 1) >= 0){
			ph.currentPos --;
			window.location.hash = ph.$links.eq(ph.currentPos).attr('href');
		}
	},
	
	
	
	onScroll: function () {
		var offSet = $('#content-slider').scrollLeft();
		$('#background').css('background-position',(-offSet/50)+'px center');
		
	},
	
	
	loadSub: function (page, params) {
			
		$mContent = $('#'+ph.rootNote+'-content').children('.content').first();
		$sContent = $('#'+ph.rootNote+'-content').children('.sub-loader');
		if ($sContent.length == 1) {
			if (ph.$currentSub == null && ph.$currentSub != $sContent)  {
				ph.closeSub();
			
				ph.$currentSub = $sContent;
				
				$mContent.stop().animate({'opacity':0},300, function () {$(this).css('display','none')});
				
				$sContent
					.css({'opacity':0, 'display':'block'})
					.stop()
					.animate({'opacity':1},250,function () {
						$.get('templates/sub_'+page+'.php',params, function (rawHtml) {
							$sContent
								.stop()
								.animate({opacity:0},250, function () {
									
									$sContent
										.removeClass('loader bg-black')
										.html(rawHtml)
										.animate({opacity:1},250);
								});
						});
					});
				
			}else{
				$sContent
					.html('')
					.addClass('loader bg-black');
				if (ph.request != null) {
					ph.request.abort();
					ph.request = null;
				}
				ph.request = $.get('templates/sub_'+page+'.php',params, function (rawHtml) {
					$sContent.html(rawHtml).removeClass('loader bg-black');
				});
			}
		
		}
	},
	
	
	
	closeSub: function () {
		if (ph.$currentSub != null) {
			$mContent = ph.$currentSub.parent().children('.content').first();
			ph.$currentSub.animate({opacity:0},250,function () {
				if (ph.$currentSub != null) ph.$currentSub.css({'display':'none'}).addClass('loader bg-black').html('');
				$mContent.css('display','block').animate({opacity:1},250);
				ph.$currentSub = null;
			});
		}
	}
	
}
