var base_url = null;
var http_base_url = null;

///////////////////////////////////////////////////////////////////////
// jQuery extensions 
///////////////////////////////////////////////////////////////////////
$.extend({
	_enc_div: null,
	htmlenc: function(val) {
		if (this._enc_div == null) this._enc_div = $('<div />');
		this._enc_div.text(val);
		return this._enc_div.html().replace(/\n/g, "<br />");
	},
	htmldec: function(val) {
		if (this._enc_div == null) this._enc_div = $('<div />');
		this._enc_div.html(val);
		return this._enc_div.text();
	},
	preload_images: function(images, callback, pointer) {
		if (pointer === undefined) pointer = 0;
		if (images.length > pointer) {
			$('<img />').load(function() {
				$.preload_images(images, callback, pointer + 1);
			}).attr('src', images[pointer]);
		} else {
			callback();
		}
	},
	intValue: function(val, min, max) {
		var v = $.trim(val);
		if (v == '') {
			return null;
		}
		v = parseInt(v, 10);
		if (isNaN(v)) {
			return null;
		}
		return Math.min(max, Math.max(min, v));
	},
	floatValue: function(val, min, max, precision) {
		if (precision === undefined) {
			precision = 2;
		}
		var v = $.trim(val);
		if (v == '') {
			return null;
		}
		v = parseFloat(v.replace(/,/, '.'));
		if (isNaN(v)) {
			return null;
		}
		var multiplier = Math.round(Math.pow(10, precision));
		return Math.min(max, Math.max(min, Math.round(v * multiplier) / multiplier));  
	}
});


// fixes the problem of assigning a null value to the controls in IE
(function(oldVal) {
	$.fn.val = function(value) {
		if (arguments.length) {
			return oldVal.call(this, (value === undefined || value === null) ? '' : value);
		} else {
			return oldVal.call(this);
		}
	};
})($.fn.val);



///////////////////////////////////////////////////////////////////////
// Page set-up
///////////////////////////////////////////////////////////////////////
var resizeTimer  = 0;
var initTimer  = 0;
$(function()
{
	var initHistory = function()
	{
		if( initTimer && menuShown )
		{
			clearTimeout(initTimer);
			$.history.init( historyController, { unescape: ",/" } );
		}
		else initTimer = setTimeout( initHistory, 100 );
	};
	initHistory();
	
	var historyController = function( hash )
	{
		if(hash == "")
		{
			// initialize app
		}
		else
		{
			//remove switcher
			if( typeof(switcher) == 'object' ) switcher.remove();
			if( typeof(switcherThumbs) == 'object' ) switcherThumbs.remove();
			if( typeof(switcherList) == 'object' ) switcherList.remove();
			//presentation layer
			if( typeof(article_presentation_layer_div) == 'object' ) article_presentation_layer_div.remove();
			if( typeof(article_presentation_layer_txt) == 'object' ) article_presentation_layer_txt.remove();
			//show content
			$('div#content_layer').show();
			//menu
			var menu = $('div#menu_layer');
			if( menu.position().top < 0 ) menu.css({ 'top': '0px' });
			
			var reg = new RegExp( '^\/?([^/]+)\/?([^/]*)\/?([^/]*)\/?.*$', 'i' );
			var a = reg.exec( hash );
			var m = a[1].toLowerCase();
			
			if( m == 'category' )
			{
				selectMenu( a[2] );
				showCategory( a[2], a[3] );
			}
			else if( m == 'article' )
			{
				selectMenu( 'a'+ a[2] );
				showArticle( a[2] );
			}
		}
	};
	
	// the page should at least consume the height of the window
	// container should be no wider than the window
	var on_window_resize = function() {
		if (resizeTimer) clearTimeout(resizeTimer);
		hideMenu();
		resizeMenu();
		resizeContent();
		resizeIntro();
		resizeTimer = setTimeout( on_window_resize_end, 500 );
	};
	var on_window_resize_end = function() {
		resizeMenu();
		showMenu();
	};
	$(window).resize(on_window_resize);//trigger('resize');
	resizeIntro();
	resizeMenu();
	resizeContent();
});

/************************* INTRO **************************/
var introCoverLoaded = false;
var introImgLoaded = false;
function initIntro( cb )
{
	var cover = (intro_mask)? intro_mask : base_url + 'css/frontend/images/intro_cover.jpg';
	introCover = $("<img src='"+ cover +"' border='0'/>").css({
		'position': 'absolute',
		'top': '-10000px',
		'left': '-10000px'
	}).load(function(){
		introCover.remove();
		introCoverLoaded = true;
		introRun( cb );
	});
	$('div#container').append( introCover );
	var img = (intro_image)? intro_image : base_url + 'css/frontend/images/intro_image.jpg';
	introImg = $("<img src='"+ img +"' border='0'/>").css({
		'position': 'absolute',
		'top': '-10000px',
		'left': '-10000px'
	}).load(function(){
		introImg.remove();
		introImgLoaded = true;
		introRun( cb );
	});
	$('div#container').append( introImg );
}

function introRun( cb )
{
	if( introCoverLoaded && introImgLoaded )
	{
		var l2 = $('div#intro_l2 img.stretch');
		$('div#intro_l1 img.stretch').delay(1100).show();
		l2.stop().delay(1000).fadeOut( 1500, cb );
	}
}

function introRemove()
{
	if( $('div#intro_l1').length > 0 )
	{
		$('div#intro_l1').remove();
	}
}

function resizeIntro()
{
	resizer.fitImage('div#intro_l1');
	resizer.fitImage('div#intro_l2');
}
/************************* INTRO **************************/

/************************* LOGO **************************/
function showLogo()
{
	if( $('div#logo').length == 0 )
	{
		var logo = $('<div id="logo"><img id="logo" src="'+ base_url +'css/frontend/images/logo.png" border="0" alt="logo image"/></div>');
		$('div#intro_l2').append( logo );
		new ToolTip( 'img#logo', {
			'path': base_url +'js/tooltip/tooltip/',
			'text': logoTooltip,
			'class': 'logo_tooltip'
		} );
		$('img#logo').bind( 'click', function() {
			$('div#intro_l1 > img').effect( 'bounce', {
				'times': 4,
				'direction': 'left',
				'distance': 8
			}, 100 );
		} );
	}
}
/************************* LOGO **************************/

/************************* MENU **************************/
var menuInitizlyzed = false;
var menuShown = false;
var menuPosition = 1; //1-in bottom; 2-in top
function initMenu( cb )
{
	var url = base_url + 'public/get_menu_html';
	$.ajax(
	{
		type: 'POST',
		dataType: "html",
		url: url,
		data: null,
		success: function( data )
		{
			menuInitizlyzed = true;
			$('div#menu_layer').append( $(data) );
			if( typeof(cb) == 'function' ) cb();
		},
		error: function( XMLHttpRequest, textStatus, errorThrown ) { alert('error get data: '+textStatus ); try {} catch (e) {} }
	});
}

function hideMenu()
{
	if( menuPosition == 1 )
	{
		var ml = $('div#menu_layer');
		ml.css( { 'visibility': 'hidden' } );
	}
	
	menuShown = false;
}

function showMenu( cb )
{
	if( menuPosition == 1 )
	{
		var ml = $('div#menu_layer');
		showMoveMenu( 0, $(window).height() + ml.height(), 0, $(window).height() - ml.height(), function() {
			menuPosition = 1;
			if( typeof(cb) == 'function' ) cb();
		} );
	}
}

function resizeMenu()
{
	var ml = $('div#menu_layer');
	var w = $(window).width();
	ml.css({
		'width': w-parseInt(ml.css('padding-left'))-parseInt(ml.css('padding-right')) +'px'
	});
}

function fadeMenu()
{
	var ml = $('div#menu_layer');
	ml.css( { 'opacity': 0 } );
	ml.css( { 'visibility': 'visible' } );
	ml.stop().animate( { 'opacity': 1 }, 1700 );
}

function showMoveMenu( initLeft, initTop, toLeft, toTop, cb )
{
	var ml = $('div#menu_layer');
	ml.css({
		'left': initLeft +'px',
		'top': initTop +'px'
	});
	ml.css( { 'visibility': 'visible' } );
	ml.stop().animate({
		'left': toLeft,
		'top': toTop
	}, 700, function() {
		menuShown = true;
		if( typeof(cb) == 'function' ) cb();
	} );
}

function moveMenu( toLeft, toTop, cb )
{
	var ml = $('div#menu_layer');
	ml.stop().animate({
		'left': toLeft,
		'top': toTop
	}, 700, function() {
		if( typeof(cb) == 'function' ) cb();
	} );
}

function selectMenu( id )
{
	if( $('div#menu div.items span#tm'+ id).length > 0 )
	{
		$('div#menu div.items span').each( function(idx) {
			$(this).removeClass('item_selected');
			$(this).addClass('item_usual');
		});
		$('div#menu div.items span#tm'+ id).addClass('item_selected');
	}
}
/************************* MENU **************************/

/************************* CONTENT **************************/
function showCategory( category_id, templateView )
{
	if( menuPosition == 1 ) //in bottom
	{
		var wh = $(window).height();
		var ml = $('div#menu_layer');
		var mh = ml.height();
		ml.css({
			'height': wh +'px'
		});
		moveMenu( 0, 0, function() {
			menuPosition = 2;
			introRemove();
			var c = $( '<div id="content_layer"><div id="content"></div></div>' );
			$('div#container').css({
				'overflow': 'visible'
			}).append( c );
			resizeContent();
			ml.css({
				'position': 'fixed',
				'height': mh +'px'
			});
			
			showCategoryContent( category_id, $('div#content'), templateView );
		} );
	}
	else
	{
		if( $('div#content').children().length > 0 )
		{
			$('div.article_presentation').fadeOut( function() {
				$(this).remove();
			});
			$('div.slides').fadeOut( function() {
				$(this).remove();
			});
			
			var content = $('div#content').children().first();
			if( $.browser.msie )
			{
				content.children().each( function(idx) {
					$(this).fadeOut();
				});
			}
			content.fadeOut( function() {
				$('div#content').children().remove();
				showCategoryContent( category_id, $('div#content'), templateView );
			} );
		}
		else
		{
			showCategoryContent( category_id, $('div#content') );
		}
	}
}

function showCategoryContent( category_id, div, templateView )
{
	if( category_id > 0 )
	{
		div = $(div);
		var loader = showContentLoader( div );
		var url = base_url +'public/get_category_html/'+ category_id;
		if( templateView ) url += '/'+ templateView;
		$.ajax(
		{
			type: 'POST',
			dataType: "html",
			url: url,
			data: null,
			success: function( data )
			{
				removeContentLoader( loader );
				div.html( data );
				resizeContent();
				var content = $('div#content').children().first();
				
				$('div.article_list_teaser div.text').each( function(idx) {
					fitText( $(this) );
				});
	
				//list view, teasers
				$('div.article_list_teaser div.readmore').hover( function() {
					$(this).animate({'color': '#b6faff'});
				}, function() {
					$(this).animate({'color': '#2fd8e4'});
				});
					
				content.hide();
				content.fadeIn( 900, function() {
					
					resizeContent();
					if( $('input[name="facebook_enable"]').first().attr('value') == 1 )
					{
						insertFacebookLike( content.children('div#content_bottom').first(), '/category/'+ category_id, (templateView==2)? false:true );
					}
				} );
			},
			error: function( XMLHttpRequest, textStatus, errorThrown ) { alert('error get data: '+textStatus ); try {} catch (e) {} }
		});
	}
}

function insertFacebookLike( div, url, hr )
{
	var completed_url = http_base_url +'#'+ $.URLEncode(url);
	var fb = '<fb:like href="'+ completed_url +'" layout="button_count" show_faces="false" width="100%" height="70px" action="like" font="trebuchet ms" colorscheme="light" />';
	if( hr ) fb = '<div class="facebook_hr">&nbsp;</div>'+ fb;
	$(div).first().before( fb );
	FB.init({
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml  : true  // parse XFBML
	});
}

function fitText( div )
{
	var div = $(div);
	var h = div.height();
	var ingress = div.children('div.ingress').first();
	if( ingress.height() + parseInt(ingress.css('padding-bottom')) > h )
	{
		if( ingress.height() > h )
		{
			//work with ingress only
			while( ingress.height() > h )
			{
				ingress.html( removeLastWord( ingress.html() ) );
			}
			ingress.html( removeLastWord( ingress.html() ) );
			ingress.html( ingress.html() +' ...' );
		}
	}
}

function fitTextWithBody( div )
{
	var div = $(div);
	var h = div.height();
	var ingress = div.children('div.ingress').first();
	var body = div.children('div.body').first();
	if( ingress.height() + parseInt(ingress.css('padding-bottom')) + body.height() > h )
	{
		if( ingress.height() > h )
		{
			//work with ingress only
			body.remove();
			while( ingress.height() > h )
			{
				ingress.html( removeLastWord( ingress.html() ) );
			}
			ingress.html( removeLastWord( ingress.html() ) );
			ingress.html( ingress.html() +' ...' );
		}
		else
		{
			while( ingress.height() + parseInt(ingress.css('padding-bottom')) + body.height() > h )
			{
				body.html( removeLastWord( body.html() ) );
			}
			body.html( removeLastWord( body.html() ) );
			body.html( body.html() +' ...' );
		}
	}
}

function removeLastWord( txt )
{
	var space1 = txt.lastIndexOf(' ');
	var space2 = txt.lastIndexOf('.');
	var space = Math.max( space1, space2 );
	return( txt.substr(0,space) );
}

function showArticle( article_id, templateView )
{
	if( menuPosition == 1 ) //in bottom
	{
		var wh = $(window).height();
		var ml = $('div#menu_layer');
		var mh = ml.height();
		ml.css({
			'height': wh +'px'
		});
		moveMenu( 0, 0, function() {
			menuPosition = 2;
			introRemove();
			var c = $( '<div id="content_layer"><div id="content"></div></div>' );
			$('div#container').css({
				'overflow': 'visible'
			}).append( c );
			resizeContent();
			ml.css({
				'position': 'fixed',
				'height': mh +'px'
			});
			
			showArticleContent( article_id, $('div#content') );
		} );
	}
	else
	{
		if( $('div#content').children().length > 0 )
		{
			$('div.article_presentation').fadeOut( function() {
				$(this).remove();
			});
			$('div.slides').fadeOut( function() {
				$(this).remove();
			});
			
			var content = $('div#content').children().first();
			if( $.browser.msie )
			{
				content.children().each( function(idx) {
					$(this).fadeOut();
				});
			}
			content.fadeOut( function() {
				$('div#content').children().remove();
				showArticleContent( article_id, $('div#content') );
			} );
			
		}
		else
		{
			showArticleContent( article_id, $('div#content') );
		}
	}
}

function showArticleContent( article_id, div )
{
	if( article_id > 0 )
	{
		div = $(div);
		var loader = showContentLoader( div );
		var url = base_url +'public/get_article_html/'+ article_id;
		$.ajax(
		{
			type: 'POST',
			dataType: "html",
			url: url,
			data: null,
			success: function( data )
			{
				removeContentLoader( loader );
				div.html( data );
				resizeContent();
				var content = $('div#content').children().first();
				content.hide();
				content.fadeIn( 900, function() {
					if( $('input[name="facebook_enable"]').first().attr('value') == 1 )
					{
						if( $('div.article_presentation').children('div#content_bottom').length > 0 )
						{
							insertFacebookLike( $('div.article_presentation').children('div#content_bottom').first(), '/article/'+ article_id, true );
						}
						else insertFacebookLike( content.children('div#content_bottom').first(), '/article/'+ article_id, true );
					}
					resizeContent();
				} );
			},
			error: function( XMLHttpRequest, textStatus, errorThrown ) { alert('error get data: '+textStatus ); try {} catch (e) {} }
		});
	}
}

function showContentLoader( div )
{
	var retval = null;
	if( $(div).length > 0 )
	{
		div = $(div);
		var loader = $('<div class="loader">'+ loaderText +'</div>');
		div.append( loader );
		
		var h = Math.min( div.height(), $(window).height() );
		
		loader.css({
			'margin-top': ( h/2-loader.height()/2 ) +'px'
		});
		
		retval = loader;
	}
	return( retval );
}

function removeContentLoader( div )
{
	if( $(div).length > 0 ) $(div).remove();
	else $('div.loader').remove();
}

function resizeContent()
{
	if( $('div#content').length > 0 )
	{
		var ml = $('div#content');
		var browserH = Math.max( $(window).height(), $(document).height() );
		var h = browserH - parseInt(ml.css('margin-top')) - parseInt(ml.css('margin-bottom')) - parseInt(ml.parent().css('padding-bottom')) - ml.parent().position().top;
		var bMarker = 0;
		if( $('div#content_bottom').length > 0 )
		{
			bMarker = $('div#content_bottom').position().top + $('div#content_bottom').height();
			var wH = $(window).height() - parseInt(ml.parent().css('padding-bottom')) - ml.parent().position().top;
			bMarker = ( bMarker > wH )? bMarker : wH;
		}
		h = ( bMarker > 0 )? Math.min( h, bMarker ) : h;
		ml.css({
			'height': h +'px'
		});
	}
	if( $('div#container').children('div.article_presentation_layer').length > 0 )
	{
		var txtLayer = $('div#container').children('div.article_presentation_layer').first();
		txtLayer.css({
			'width': $(window).width() +'px'
		});
	}
	
	$('#container .slides img.image_to_fit').each( function(e) {
		resizer.fitImage( $(this).parent() );
	});
}

function onMouseOverThumb( div, text )
{
	if( $(div).length > 0 && $(div).children('div.cover').length == 0 )
	{
		var cover = $('<div class="cover"><div class="text">'+ text +'<div style="margin-top:5px"><img src="'+ base_url +'css/frontend/images/btn_arrow_right_black.png"/></div></div></div>');
		$(div).append( cover );
		var t = cover.children('div.text').first();
		cover.stop().animate({'opacity': 0.9});
		var id = $(div).attr('tag');
		$('div#article_thumb_header').html('<div>'+ $('div#heading_'+ id).html() +'</div>');
	}
}

function onMouseOutThumb( div, event )
{
	if( $(div).length > 0 )
	{
		var target = event.relatedTarget || event.toElement;
		if( ifNotParent( target, div ) )
		{
			$(div).children('div.cover').stop().animate( {'opacity': 0}, function() { $(div).children('div.cover').remove(); });
			$('div#article_thumb_header').html('<div>SELECT <br />A PROJECT</div>');
		}
	}
}

function ifNotParent( target, div )
{
	var retval = true;
	while( target != null )
	{
		if( target == div )
		{
			retval = false;
			break;
		}
		target = target.parentNode;
	}
	
	return( retval );
}

function iaGoto( a, slider, idx )
{
	$(a).parent().children('a').removeClass('selected');
	$(a).addClass('selected');
	slider.play( idx );
}

function iaSliderOnClick( div, slider, event )
{
	if(!event) var event = window.event;
	var target = event.target || event.srcElement;
	
	if( $(div).length > 0 )
	{
		div = $(div);
		var w = div.width();
		var h = div.height();
		
		var prev = div.children('.nav_prev').first();
		var next = div.children('.nav_next').first();
		var prevIdx = 0;
		var nextIdx = 0;
		var length = slider.slides.length;
		
		target = $(target);
		var mLeft = event.offsetX || event.layerX;
		var p = target;
		while( p && p.attr('id') != div.attr('id') )
		{
			mLeft += p.position().left;
			p = p.parent();
		}
		
		var direction = 0;
		if( mLeft <= w/3 ) direction = -1;
		else if( mLeft > w*0.66 ) direction = 1;
		
		if( direction == -1 )
		{
			//West
			slider.prev();
			prevIdx = slider.currentIdx - 1;
			if( prevIdx+1 == 0 ) prev.css('visibility','hidden');
			else prev.html( prevIdx+1 +'/'+ length );
		}
		else if( direction == 1 )
		{
			//East
			slider.next();
			nextIdx = slider.currentIdx + 1;
			if( nextIdx+1 > length ) next.css('visibility','hidden');
			else next.html( nextIdx+1 +'/'+ length );
		}
		
		if( div.next('div.media_paginator').length > 0 )
		{
			div.next('div.media_paginator').first().children('a').removeClass('selected');
			div.next('div.media_paginator').first().children('a').eq(slider.currentIdx).addClass('selected');
		}
	}
}

var iaSliderOnMouseMoveRun = false;
function iaSliderOnMouseMove( div, slider, event )
{
	if(!event) var event = window.event;
	var target = event.target || event.srcElement;
	if( !iaSliderOnMouseMoveRun && $(div).length > 0 )
	{
		iaSliderOnMouseMoveRun = true;
		
		div = $(div);
		var w = div.width();
		var h = div.height();
		
		target = $(target);
		var mLeft = event.offsetX || event.layerX;
		var mTop = event.offsetY || event.layerY;
		var p = target;
		while( p.attr('id') != div.attr('id') )
		{
			if( $.browser.msie )
			{
				if( p.attr('class') == 'nav_next' )
				{
					mTop = p.position().top + target.height()/2;
					mLeft = Math.max( mLeft, p.position().left );
					mLeft -= p.width();
				}
				else if( p.attr('class') == 'nav_prev' )
				{
					mTop = p.position().top + target.height();
					mLeft -= target.width()/2;
				}
				else
				{
					mLeft += p.position().left;
					mTop += p.position().top;
				}
			}
			else
			{
				mLeft += p.position().left;
				mTop += p.position().top;
			}
			
			p = p.parent();
		}
		
		var prev = div.children('.nav_prev').first();
		var next = div.children('.nav_next').first();
		
		if( !mTop ) mTop = 0;
		if( !mLeft ) mLeft = 0;
		/*
		if( $.browser.msie && (target.attr('class') == 'nav_next' || target.attr('class') == 'nav_prev') )
		{
			mLeft -= next.width()/2;
			mTop -= next.height();
		}*/
		
		var direction = 0;
		if( mLeft <= w/3 ) direction = -1;
		else if( mLeft > w*0.66 ) direction = 1;
		
		var prevIdx = slider.currentIdx - 1;
		var nextIdx = slider.currentIdx + 1;
		var length = slider.slides.length;
		
		if( direction == -1 && prevIdx >= 0 )
		{
			//West
			prev.html( prevIdx+1 +'/'+ length );
			prev.css({
				'visibility': 'visible',
				'left': 50,
				'top': Math.floor( h/2 - prev.height() )
			});
			
			next.css({ 'visibility': 'hidden' });
		}
		else if( direction == 1 && nextIdx < length )
		{
			//East
			next.html( nextIdx+1 +'/'+ length );
			next.css({
				'visibility': 'visible',
				'right': Math.floor( 50 + next.width() ),
				'top': Math.floor( h/2 - next.height() )
			});
			
			prev.css({ 'visibility': 'hidden' });
		}
		else
		{
			var tClass = target.attr('class');
			if( tClass != prev.attr('class') && tClass != next.attr('class') )
			{
				div.removeClass('nocursor');
				prev.css({ 'visibility': 'hidden' });
				next.css({ 'visibility': 'hidden' });
			}
		}
		
		iaSliderOnMouseMoveRun = false;
	}
}

function iaSliderOnMouseOut( div, slider, event )
{
	if(!event) var event = window.event;
	var target = event.target || event.srcElement;
	div = $(div);
	div.removeClass('nocursor');
	div.children('.nav_prev').first().css({ 'visibility': 'hidden' });
	div.children('.nav_next').first().css({ 'visibility': 'hidden' });
}

function paHeadingOnClick( div, slider, event )
{
	if(!event) var event = window.event;
	var target = event.target || event.srcElement;
	
	var menu = $('div#menu_layer');
	var menuHeight = 0;
	if( menu.position().top == 0 ) menuHeight = menu.height();
	
	var txtLayer = $('div#container').children('div.article_presentation_layer').first();
	txtLayer.css({
		'top': slider.getEl().height()+1 +'px',
		'width': $(window).width() +'px',
		'display': 'block'
	});
	txtLayer.animate( {'top': menuHeight}, 600, function() {
		var h = Math.max( txtLayer.height(), txtLayer.children().first().height()+parseInt(txtLayer.children().first().css('padding-bottom'))+parseInt(txtLayer.children().first().css('margin-top')) );
		txtLayer.css({
			'height': h +'px'
		});
	} );
}

function paArrowOnClick( div, slider, event )
{
	if(!event) var event = window.event;
	var target = event.target || event.srcElement;
	var txtLayer = $('div#container').children('div.article_presentation_layer').first();
	txtLayer.animate( {'top': slider.getEl().height()+1}, 600, function() {
		txtLayer.css({
			'display': 'none'
		});
	} );
}

function paSliderOnClick( div, slider, event )
{
	// XXX: debug
//	if (window.console) console.log('in paSliderOnClick');

	if(!event) var event = window.event;
	var target = event.target || event.srcElement;
	var txtLayer = $('div#container').children('div.article_presentation_layer').first();
	if( $(div).length > 0 && target.className != 'heading' && target.parentNode.className != 'heading' && txtLayer.css('display') == 'none' )
	{
		div = $(div);
		var w = div.width();
		var h = div.height();
		
		var prev = div.children('.nav_prev').first();
		var next = div.children('.nav_next').first();
		var prevIdx = 0;
		var nextIdx = 0;
		var length = slider.slides.length;
		
		target = $(target);
		var mLeft = event.offsetX || event.layerX;
		var mTop = event.offsetY || event.layerY;
		var p = target;
		while( p && p.attr('id') != div.attr('id') )
		{
			if( mLeft < p.position().left ) mLeft += p.position().left;
			if( mTop < p.position().top ) mTop += p.position().top;
			p = p.parent();
		}
		
		var direction = 0;
		
		if( mLeft <= w/3 ) direction = -1;
		else if( mLeft > w*0.66 ) direction = 1;
		else if( mTop <= h/3 ) direction = -2;
		else if( mTop > h*0.66 ) direction = 2;
		
		if( direction == -1 )
		{
			//West
			slider.prev();
			prevIdx = slider.currentIdx - 1;
			if( prevIdx+1 == 0 ) prev.css('visibility','hidden');
			else prev.html( prevIdx+1 +'/'+ length );
		}
		else if( direction == 1 )
		{
			//East
			slider.next();
			nextIdx = slider.currentIdx + 1;
			if( nextIdx+1 > length ) next.css('visibility','hidden');
			else next.html( nextIdx+1 +'/'+ length );
		}
		else if( direction == -2 )
		{
			//North
			var menu = $('div#menu_layer');
			if( menu.position().top == 0 ) menu.animate({ 'top': -1*menu.height() });
			else menu.animate({ 'top': 0 });
			
			var heading = div.children('div.heading');
			heading.toggle();
		}
		else if( direction == 2 )
		{
			//South
			var back = $('div.nav_back');
			var id = 0;
			id = parseInt( back.attr('tag') );
			if( id > 0 ) $.history.load( '/category/'+ id );
		}
	}
}

function paSliderOnMouseMove( div, slider, event )
{
	// XXX: debug
//	if (window.console) console.log('in paSliderOnMouseMove');

	if(!event) var event = window.event;
	var target = event.target || event.srcElement;
	if( $(div).length > 0 && target.className != 'heading' && target.parentNode.className != 'heading' )
	{
		div = $(div);
		var w = div.width();
		var h = div.height();
		
		target = $(target);
		var mLeft = event.offsetX || event.layerX;
		var mTop = event.offsetY || event.layerY;
		var p = target;
		while( p && p.attr('id') != div.attr('id') )
		{
			if( mLeft < p.position().left ) mLeft += p.position().left;
			if( mTop < p.position().top ) mTop += p.position().top;
			p = p.parent();
		}
		
		var direction = 0;
		
		if( mLeft <= w/3 ) direction = -1;
		else if( mLeft > w*0.66 ) direction = 1;
		else if( mTop <= h/3 ) direction = -2;
		else if( mTop > h*0.66 ) direction = 2;
		else if( mTop > h/2-100 && mTop < h/2+100 && mLeft > w/2-100 && mLeft < w/2+100 ) direction = 3;
		
		var prevIdx = slider.currentIdx - 1;
		var nextIdx = slider.currentIdx + 1;
		var length = slider.slides.length;
		
		var prev = div.children('.nav_prev').first();
		var next = div.children('.nav_next').first();
		var menu = div.children('.nav_menu').first();
		var back = div.children('.nav_back').first();
		
		var pause = slider.getEl().children("div[class^=\'btn_pause\']").first();
		
		if( direction == -1 && prevIdx >= 0 && length > 1 )
		{
			//West
			// XXX: debug
			if (window.console) console.log('in paSliderOnMouseMove: west');

			prev.html( prevIdx+1 +'/'+ length );
			prev.css({
				'visibility': 'visible'
			});
			
			next.css({ 'visibility': 'hidden' });
			menu.css({ 'visibility': 'hidden' });
			back.css({ 'visibility': 'hidden' });
			pause.hide();
		}
		else if( direction == 1 && nextIdx < length && length > 1 )
		{
			//East
			// XXX: debug
			if (window.console) console.log('in paSliderOnMouseMove: east');

			next.html( nextIdx+1 +'/'+ length );
			next.css({
				'visibility': 'visible'
			});
			
			prev.css({ 'visibility': 'hidden' });
			menu.css({ 'visibility': 'hidden' });
			back.css({ 'visibility': 'hidden' });
			pause.hide();
		}
		else if( direction == -2 )
		{
			//North
			// XXX: debug
			if (window.console) console.log('in paSliderOnMouseMove: north');

			menu.css({
				'visibility': 'visible',
				'left': Math.floor(w/2 - menu.width()/2),
				'top': 100
			});

			prev.css({ 'visibility': 'hidden' });
			next.css({ 'visibility': 'hidden' });
			back.css({ 'visibility': 'hidden' });
			pause.hide();
		}
		else if( direction == 2 )
		{
			//South
			// XXX: debug
			if (window.console) console.log('in paSliderOnMouseMove: south');

			back.css({
				'visibility': 'visible',
				'left': Math.floor(w/2 - menu.width()/2),
				'bottom': 50 + back.height() 
			});

			prev.css({ 'visibility': 'hidden' });
			next.css({ 'visibility': 'hidden' });
			menu.css({ 'visibility': 'hidden' });
			pause.hide();
		}
		else if( direction == 3 ) //play button
		{
			if( slider.slidesData[slider.currentIdx].ext == 'url' || slider.slidesData[slider.currentIdx].ext == 'flv' )
			{
				if( pause.css('visibility') == 'visible' )
				{
					pause.show();
				}
			}
		}
		else
		{
			var tClass = target.attr('class');
			if( tClass != prev.attr('class') && tClass != next.attr('class') && tClass != menu.attr('class') && tClass != back.attr('class') )
			{
				// XXX: debug
				if (window.console) console.log('in paSliderOnMouseMove: else 1');

				div.removeClass('nocursor');
				prev.css({ 'visibility': 'hidden' });
				next.css({ 'visibility': 'hidden' });
				menu.css({ 'visibility': 'hidden' });
				back.css({ 'visibility': 'hidden' });
				pause.hide();
			}
		}
	}
	else
	{
		div = $(div);

		// XXX: debug
		if (window.console) console.log('in paSliderOnMouseMove: else 2');


		var prev = div.children('.nav_prev').first();
		var next = div.children('.nav_next').first();
		var menu = div.children('.nav_menu').first();
		var back = div.children('.nav_back').first();
		div.removeClass('nocursor');
		prev.css({ 'visibility': 'hidden' });
		next.css({ 'visibility': 'hidden' });
		menu.css({ 'visibility': 'hidden' });
		back.css({ 'visibility': 'hidden' });
	}
}

function paSliderOnMouseOut( div, slider, event )
{
	// XXX: debug
//	if (window.console) console.log('in paSliderOnMouseOut');

	if(!event) var event = window.event;
	var target = event.target || event.srcElement;
	target = $(target);
	div = $(div);
	
	var prev = div.children('.nav_prev').first();
	var next = div.children('.nav_next').first();
	var menu = div.children('.nav_menu').first();
	var back = div.children('.nav_back').first();
	
	var tClass = target.attr('class');
	if( tClass != div.attr('class') && tClass != prev.attr('class') && tClass != next.attr('class') && tClass != menu.attr('class') && tClass != back.attr('class') )
	{
		div.removeClass('nocursor');
		$('.nav_prev:eq(0),.nav_next:eq(0),.nav_menu:eq(0),.nav_back:eq(0)', div).css({ 'visibility': 'hidden' });
	}
}
/************************* CONTENT **************************/

/************************* COMMON **************************/
var resizer = {
	'fitImage': function( el ) {

		var wW = $(window).width();
		var wH = $(window).height();
		var wK = wW/wH;

		var a = $(el).parents(":hidden");
		a.each(function () {
			$(this).data('opacity', $(this).css('opacity'));
			$(this).data('display', $(this).css('display'));
			$(this).css({
				'opacity': 0,
				'display': 'block'
			});
		});

		var l = 0, t = 0;
		$(el).each(function () {
			var f = $("img", this).first(),
			iW = f.data("width") || f.width(),
			iH = f.data("height") || f.height();
			if( iW && iH ) {
				iK = iW / iH;
				f.data("width", iW);
				f.data("height", iH);
				if( iK > wK ) {
					l = -Math.floor((wH * iK - wW) / 2);
					f.css({
						'width': "auto",
						'height': wH,
						'left': l,
						'top': 0
					});
				}
				else {
					t = -Math.floor((wW / iK - wH) / 2);
					f.css({
						'width': wW,
						'height': "auto",
						'top': t,
						'left': 0
					});
				}
				$(this).css({
					'width': wW,
					'height': wH/*,
					display: "block"*/
				});
			}
		});
		
		a.each(function () {
			$(this).css({
				'opacity': $(this).data('opacity') || 1,
				'display': $(this).data('display')
			});
		});
	}
};
/************************* COMMON **************************/

