var body_content = '', buffer = '', buffer_html = '', webcam_width = 240, webcam_height = 180, is_working = false, current_page = '', header_img = '', header_ready = false, content_ready = false, is_locked = false, scroll_animate = false;

$(document).ready(function () {
	buffer = $('#main_content').html();
	$('#main_content').html('<div id="buffer" style="display:none;position:absolute;"></div><div id="main_body_content" style="position:absolute;background:#fff;">' + buffer + '</div>');

	initMenu();
	initLinks();
	initSubLinks();

	$(document).ajaxSend(function () {
		is_locked	= true;
	});

	$(document).ajaxComplete(function () {
		is_locked	= false;
	});

	$(document).ajaxError(function () {
		is_locked	= false;
	});

	$(document).ajaxStop(function () {
		is_locked	= false;
	});
});

function renderPage () {
	if (!(header_ready && content_ready))
		return;

	$('#main_body_content').fadeOut(1000, function () {
		$(this).html($('#buffer').html());

		$(this).show();

		initMenu();
		initLinks();
		initSubLinks();

		$('#buffer').html('');
		$('#buffer').hide();

		content_ready	= false;
	});

	$('#header_image').find('img').fadeOut(1000, function () {
		$(this).attr('src', header_img);
		$(this).show();

		header_ready	= false;
	});
};

function initMenu () {
	$('.bottom_menu').each(function (i) {
		$(this).attr('rel', 'num_' + i);
	});

    $('.main_menu').click(function (e) {
		e.preventDefault();

		if (is_locked) return;

		if (current_page != $(this).attr('href') && !(header_ready && content_ready))
			current_page	= $(this).attr('href');
		else
			return;

		$.getJSON($(this).attr('href') + '/ajax/actions/get_body', function (data) {
			buffer_html = '';

			$('#buffer').html(data.body);
			$('#buffer').show();

			content_ready	= true;
			renderPage();
		});

		$.getJSON($(this).attr('href') + '/ajax/actions/get_header', function (data) {
			var img_url = base_url + 'images/' + data.header;

			document.title	= data.title;

			$('#header_image').css({ 'background': 'transparent url(\'' + img_url + '\') top left no-repeat',
									 'display': 'block',
									 'width': 986,
									 'height': 274,
									 'padding-bottom': 3 });

			header_img	= img_url;

			header_ready	= true;
			renderPage();
		});
    });

	$('.event_calendar_day').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		extraClass: "pretty",
		fixPNG: true,
		opacity: 0.95,
		left: -120
	});
}

function initLinks () {
	var $body_content = $('#main_body_content'), padLeft = '20px', padRight = '20px', defpadLeft = $('.menu li a').css('paddingLeft'), defpadRight = $('.menu li a').css('paddingRight');

	/* $('.sidebar').mousemove(function(e) {
		var s_top = parseInt($(this).offset().top), s_bottom = parseInt($(this).height() + s_top), mheight = parseInt(($(this).find('.menu li').height() + 20) * $(this).find('.menu li').length), top_value = Math.round(((s_top - e.pageY) / 100) * mheight / 1);

		if (Math.abs(top_value) > (mheight - $(this).height()))
			top_value	= 0 - (mheight - $(this).height());

		$(this).find('.menu').animate({top: top_value}, { queue:false, duration:6000});
	}); */

	$('.arrow_container').find('a.move_down').hover(function () {
		scroll_animate	= true;
		scrollDown($(this).parent().parent().parent().find('.sidebar'));
	}, function () {
		scroll_animate	= false;
	});

	$('.arrow_container').find('a.move_up').hover(function () {
		scroll_animate	= true;
		scrollUp($(this).parent().parent().parent().find('.sidebar'));
	}, function () {
		scroll_animate	= false;
	});

	$('.sidebar').css({
		'overflow-y':'hidden'
	});

	$('.sidebar2').css({
		'overflow-y':'hidden'
	});

	$body_content.find('#reset').click(function (e) {
		e.preventDefault();

		$('#name').val('');
		$('#message').val('');
		$('#pixel').val('');
	});

	if (typeof $body_content.find('#add_contact') == 'object') {
		$body_content.find('#add_contact').click(function (e) {
			e.preventDefault();

			if (is_locked) return;

			$.post($body_content.find('#form_contact').attr('action'), {
				contact_name  : $body_content.find('#contact_name').val(),
				contact_email : $body_content.find('#contact_email').val(),
				contact_subject : $body_content.find('#contact_subject').val(),
				contact_code  : $body_content.find('#contact_code').val(),
				contact_text  : $body_content.find('#contact_text').val()
			}, function (data) {
				if(data == 'success') {
					alert('your contact has been sent');
					clearFields();
				} else {
					alert(data);
				}
			});
		});
	}

	if (typeof $body_content.find('#clear_contact') == 'object') {
		$body_content.find('#clear_contact').click(function (e) {
			e.preventDefault();
			clearFields();
		});
	}

	function clearFields () {
		$('#contact_name').val('');
		$('#contact_email').val('');
		$('#contact_subject').val('');
		$('#contact_code').val('');
		$('#contact_text').val('');
	}

	$body_content.find('#news_prev').click(function (e) {

		if (is_locked) return;

		$.getJSON(site_url + '/news_events/ajax/actions/prev_date/date/' + $body_content.find('#content_title_container_body_left_body_news').html(), function (data) {
			$body_content.find('#content_title_container_body_left_body_news').html(data.date);

			buffer_html	= '';

			jQuery.each(data.articles, function (index, value) {
				buffer_html += '<div class="content_container_body_left_body_news_row">'
							+  '<div class="content_container_body_left_body_news_row_left">'
							+  '<div class="content_container_body_left_body_news_row_left_title">'
							+  value.date
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_left_title_pic">'
							+  '<img src="' + base_url + value.image + '" />'
							+  '</div>'
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_right">'
							+  '<div class="content_container_body_left_body_news_row_right_title">'
							+  '<a href="' + site_url + '/news_events/actions/read/article/' + value.id + '" rel="' + value.id + '" class="read_article">' + value.subject + '</a>'
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_right_content">'
							+  value.synopsis
							+  '</div>'
							+  '</div>'
							+  '</div>';
			});

			if (buffer_html == '')
				buffer_html	= '<p>Data not available</p>';

			$body_content.find('#content_container_body_left_body_news').html(buffer_html);

			initSubLinks();
		});
	});

	$body_content.find('#news_next').click(function (e) {

		if (is_locked) return;

		$.getJSON(site_url + '/news_events/ajax/actions/next_date/date/' + $body_content.find('#content_title_container_body_left_body_news').html(), function (data) {
			$body_content.find('#content_title_container_body_left_body_news').html(data.date);

			buffer_html	= '';

			jQuery.each(data.articles, function (index, value) {
				buffer_html += '<div class="content_container_body_left_body_news_row">'
							+  '<div class="content_container_body_left_body_news_row_left">'
							+  '<div class="content_container_body_left_body_news_row_left_title">'
							+  value.date
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_left_title_pic">'
							+  '<img src="' + base_url + value.image + '" />'
							+  '</div>'
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_right">'
							+  '<div class="content_container_body_left_body_news_row_right_title">'
							+  '<a href="' + site_url + '/news_events/actions/read/article/' + value.id + '" rel="' + value.id + '" class="read_article">' + value.subject + '</a>'
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_right_content">'
							+  value.synopsis
							+  '</div>'
							+  '</div>'
							+  '</div>';
			});

			if (buffer_html == '')
				buffer_html	= '<p>Data not available</p>';

			$body_content.find('#content_container_body_left_body_news').html(buffer_html);

			initSubLinks();
		});
	});

	$body_content.find('#link_news').click(function (e) {
		e.preventDefault();

		if (is_locked) return;

		$('#main_body_content').find('#link_events').css({
			'background': '#fff url(\'' + base_url + 'images/events_tab_but_def.png' + '\') top left no-repeat',
			'margin-left': 0
		});

		$('#main_body_content').find('#link_news').css({
			'background': '#fff url(\'' + base_url + 'images/news_tab_but.png' + '\') top left no-repeat',
			'margin-left': 0
		});

		$.getJSON(site_url + '/news_events/ajax/actions/tab_news', function (data) {
			buffer_html	= '';

			jQuery.each(data.articles, function (index, value) {
				buffer_html += '<div class="content_container_body_left_body_news_row">'
							+  '<div class="content_container_body_left_body_news_row_left">'
							+  '<div class="content_container_body_left_body_news_row_left_title">'
							+  value.date
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_left_title_pic">'
							+  '<img src="' + base_url + value.image + '" />'
							+  '</div>'
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_right">'
							+  '<div class="content_container_body_left_body_news_row_right_title">'
							+  '<a href="' + site_url + '/news_events/news/' + value.id + '" rel="' + value.id + '" class="read_article">' + value.subject + '</a>'
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_right_content">'
							+  value.synopsis
							+  '</div>'
							+  '</div>'
							+  '</div>';
			});

			if (buffer_html == '')
				buffer_html	= '<p>Data not available</p>';

			$body_content.find('#content_container_body_left_body_news').html(buffer_html);

			initSubLinks();
		});
	});

	$body_content.find('#link_events').click(function (e) {
		e.preventDefault();

		if (is_locked) return;

		$('#main_body_content').find('#link_events').css({
			'background': '#fff url(\'' + base_url + 'images/events_tab_but.png' + '\') top left no-repeat',
			'margin-left': 0
		});

		$('#main_body_content').find('#link_news').css({
			'background': '#fff url(\'' + base_url + 'images/news_tab_but_def.png' + '\') top left no-repeat',
			'margin-left': 0
		});

		$.getJSON(site_url + '/news_events/ajax/actions/tab_events', function (data) {
			buffer_html	= '';

			jQuery.each(data.articles, function (index, value) {
				buffer_html += '<div class="content_container_body_left_body_news_row">'
							+  '<div class="content_container_body_left_body_news_row_left">'
							+  '<div class="content_container_body_left_body_news_row_left_title">'
							+  value.date
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_left_title_pic">'
							+  '<img src="' + base_url + value.image + '" />'
							+  '</div>'
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_right">'
							+  '<div class="content_container_body_left_body_news_row_right_title">'
							+  '<a href="' + site_url + '/news_events/events/' + value.id + '" rel="' + value.id + '" class="read_article">' + value.subject + '</a>'
							+  '</div>'
							+  '<div class="content_container_body_left_body_news_row_right_content">'
							+  value.synopsis
							+  '</div>'
							+  '</div>'
							+  '</div>';
			});

			if (buffer_html == '')
				buffer_html	= '<p>Data not available</p>';

			$body_content.find('#content_container_body_left_body_news').html(buffer_html);

			initSubLinks();
		});
	});

	$body_content.find('.project_tabs').click(function (e) {
		e.preventDefault();

		var tab_name = $(this).attr('rel');

		$(this).parent().parent().find('.project_tabs').each(function () {
			$(this).find('img').attr('src', base_url + 'images/' + $(this).attr('rel') + '_tab_def.png');
		});

		$(this).find('img').attr('src', base_url + 'images/' + $(this).attr('rel') + '_tab_act.png');

		$body_content.find('#link_left_content_project').css({
			background: 'transparent url(\'' + base_url + 'images/' + tab_name + '-left-content.jpg\') left top no-repeat'
		});

		$.getJSON(site_url + '/projects/ajax/actions/tab_' + tab_name, function (data) {
			buffer_html = '<ul>';

			jQuery.each(data.articles, function (index, value) {
				buffer_html += '<li><a href="' + site_url + '/projects/index/' + tab_name + '/' + value.name + '" class="project_detail" rel="' + value.id + '">' + value.subject + '</a></li>';
			});

			buffer_html += '</ul>';

			$body_content.find('#container_left_content_project').html(buffer_html);

			initSubLinks();
		});
	});

	$('.scroll-pane, .scroll-pane-au, .scroll-pane-ne, scroll-pane-cp').jScrollPane({showArrows:true});
}

function initSubLinks () {

	var $body_content = $('#main_body_content'), html_buffer = '';

	/** news & events **/

	$body_content.find('.read_article').click(function (e) {
		e.preventDefault();

		if (is_locked) return;

		$.getJSON(site_url + '/news_events/ajax/actions/read/article/' + $(this).attr('rel'), function (data) {
			html_buffer = '<div id="right_event_title">'
						+ '<div id="date_news_title">' + data.date + '</div>'
						+ '<div id="title_news">' + data.subject + '</div>'
						+ '<div class="clear"></div>'
						+ '</div>'
						+ '<div id="right_event_pic">'
						+ '<img src="' + data.image + '" alt="480x180px" />'
						+ '</div>'
						+ '<div id="right_event_text" class="scroll-pane-ne">' + data.text + '</div>'
						+ '<div id="right_event_share">'
						+ '<div id="right_event_share_title">Share : </div><div id="right_event_share_link">'
						+ '<a href="#" class="facebook">Facebook</a> '
						+ '<a href="#" class="twitter">Twitter</a>'
						+ '</div>'
						+ '</div>';

			$body_content.find('#right_body_news').html(html_buffer);
		});
	});

	/** Projects **/

	$body_content.find('.project_detail').click(function (e) {
		e.preventDefault();

		if (is_locked) return;

		$.getJSON(site_url + '/projects/ajax/actions/read/article/' + $(this).attr('rel'), function (data) {
			$body_content.find('#top_right_content_project').html('<img src="' + base_url + 'images/' + data.category + '-top-content-project.png" alt=""/>');
			$body_content.find('#project_date').html(data.date);
			$body_content.find('#project_title').html(data.subject);

			$body_content.find('#project_date').removeAttr('style');
			$body_content.find('#project_title').removeAttr('style');
			$body_content.find('#project_content_pic').removeAttr('style');

			if (data.file_type == 'image') {
				$body_content.find('#project_content_pic').html('<img border="0" src="' + base_url + 'uploads/articles/' + data.file_name + '" />');;
			} else if (data.file_type == 'movie') {
				movie_html	= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="320" height="240" id="FLVPlayer">'
							+ '<param name="movie" value="' + base_url + 'flash/FLVPlayer_Progressive.swf" />'
							+ '<param name="quality" value="high" />'
							+ '<param name="wmode" value="transparent" />'
							+ '<param name="scale" value="noscale" />'
							+ '<param name="salign" value="lt" />'
							+ '<param name="FlashVars" value="skinName=' + base_url + 'flash/Corona_Skin_2&amp;streamName=' + base_url + 'uploads/articles/' + data.file_name + '&amp;autoPlay=false&amp;autoRewind=false" />'
							+ '<param name="swfversion" value="8,0,0,0" />'
							+ '<param name="expressinstall" value="Scripts/expressInstall.swf" />'
							+ '<object type="application/x-shockwave-flash" data="' + base_url + 'flash/FLVPlayer_Progressive.swf" width="320" height="240">'
							+ '<param name="quality" value="high" />'
							+ '<param name="wmode" value="transparent" />'
							+ '<param name="scale" value="noscale" />'
							+ '<param name="salign" value="lt" />'
							+ '<param name="FlashVars" value="skinName=' + base_url + 'flash/Corona_Skin_2&amp;streamName=' + base_url + 'uploads/articles/' + data.file_name + '&amp;autoPlay=false&amp;autoRewind=false" />'
							+ '<param name="swfversion" value="8,0,0,0" />'
							+ '<param name="expressinstall" value="Scripts/expressInstall.swf" />'
							+ '<div><h4>Content on this page requires a newer version of Adobe Flash Player.</h4><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>'
							+ '</object>'
							+ '</object>';
				$body_content.find('#project_content_pic').html(movie_html);
			} else if (data.file_type == 'music') {
				movie_html	= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="320" height="20" id="FLVPlayer">'
							+ '<param name="movie" value="' + base_url + 'flash/singlemp3player.swf" />'
							+ '<param name="quality" value="high" />'
							+ '<param name="wmode" value="transparent" />'
							+ '<param name="scale" value="noscale" />'
							+ '<param name="salign" value="lt" />'
							+ '<param name="FlashVars" value="file=' + base_url + 'uploads/articles/' + data.file_name + '&amp;backColor=c2c2c2&amp;frontColor=666666&amp;showDownload=false&amp;repeatPlay=false&songVolume=100" />'
							+ '<param name="swfversion" value="8,0,0,0" />'
							+ '<param name="expressinstall" value="Scripts/expressInstall.swf" />'
							+ '<object type="application/x-shockwave-flash" data="' + base_url + 'flash/singlemp3player.swf" width="320" height="240">'
							+ '<param name="quality" value="high" />'
							+ '<param name="wmode" value="transparent" />'
							+ '<param name="scale" value="noscale" />'
							+ '<param name="salign" value="lt" />'
							+ '<param name="FlashVars" value="file=' + base_url + 'uploads/articles/' + data.file_name + '&amp;backColor=c2c2c2&amp;frontColor=666666&amp;showDownload=false&amp;repeatPlay=false&songVolume=100" />'
							+ '<param name="swfversion" value="8,0,0,0" />'
							+ '<param name="expressinstall" value="Scripts/expressInstall.swf" />'
							+ '<div><h4>Content on this page requires a newer version of Adobe Flash Player.</h4><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>'
							+ '</object>'
							+ '</object>';
				$body_content.find('#project_content_pic').html(movie_html);
			} else if (data.file_type == '') {
				$body_content.find('#project_content_pic').html('<div style="color:#e19e1d;font-size:26px;margin:120px 0px 0px 0px;">no image/video <br/> available</div>');
			}

			$body_content.find('#project_content_detailed').html(data.text);
		});
	});

	/** careers **/

	$body_content.find('.read_details').click(function (e) {
		e.preventDefault();

		if (is_locked) return;

		$.getJSON(site_url + '/careers/ajax/actions/read/article/' + $(this).attr('rel'), function (data) {
			html_buffer	= '<h2>' + data.subject + '</h2>'
						+ data.text;

			$body_content.find('#right_content_body_career_body_content').html(html_buffer);
			$('#right_content_body_career').fadeIn();
		});
	});

	/** client & partners **/

	$body_content.find('.client_details').click(function (e) {
		e.preventDefault();

		if (is_locked) return;

		$.getJSON(site_url + '/clients_partners/ajax/actions/read/article/' + $(this).attr('rel'), function (data) {
			html_buffer = '<div id="right_body_client_container_title">' + data.subject + '</div>'
						+ '<div id="right_body_client_container_pic">'
						+ '<img src="' +data.image+ '" alt="480x180px" />'
						+ '</div>'
						+ '<div id="right_body_client_container_desc">' + data.text + '</div>'

			$body_content.find('#right_body_client_container').html(html_buffer);
		});
	});

	$('.scroll-pane, .scroll-pane-au, .scroll-pane-ne, scroll-pane-cp').jScrollPane({showArrows:true});
}

function scrollDown (sidebar) {
	if (!scroll_animate) return;

	if (is_locked) return;

	is_locked	= true;

	//var s_top = parseInt(sidebar.offset().top), s_bottom = parseInt(sidebar.find('.sidebar').height() + s_top), mheight = parseInt((sidebar.find('.menu li').height() + 20) * sidebar.find('.menu li').length);
	var top_value = 0,mheight = parseInt((sidebar.find('.menu li').height() + 20) * sidebar.find('.menu li').length);

	if (Math.abs(parseInt(sidebar.find('.menu').css('top').replace('px', '')) - ((sidebar.find('.menu li').height() + 20))) <= (mheight - sidebar.height() + (sidebar.find('.menu li').height() + 20)))
		top_value	= parseInt(sidebar.find('.menu').css('top').replace('px', '')) - ((sidebar.find('.menu li').height() + 20));

	sidebar.find('.menu').animate({top: top_value}, { queue:false, duration:1000, complete:function () { is_locked = false; scrollDown(sidebar); }});
}

function scrollUp (sidebar) {
	if (!scroll_animate) return;

	if (is_locked) return;

	is_locked	= true;

	//var s_top = parseInt(sidebar.offset().top), s_bottom = parseInt(sidebar.find('.sidebar').height() + s_top), mheight = parseInt((sidebar.find('.menu li').height() + 20) * sidebar.find('.menu li').length);
	var top_value = 0,mheight = parseInt((sidebar.find('.menu li').height() + 20) * sidebar.find('.menu li').length);

	if (parseInt(sidebar.find('.menu').css('top').replace('px', '')) + ((sidebar.find('.menu li').height() + 20)) < 0 + (sidebar.find('.menu li').height() + 20))
		top_value	= parseInt(sidebar.find('.menu').css('top').replace('px', '')) + ((sidebar.find('.menu li').height() + 20));

	sidebar.find('.menu').animate({top: top_value}, { queue:false, duration:1000, complete:function () { is_locked = false; scrollUp(sidebar); }});
}

