//jfade
(function($, document){
	$.fn.jfade = function(settings) {
		var defaults = {
			start_opacity: "1",
			high_opacity: "1",
			low_opacity: ".6",
			timing: "500"
		};
		var settings = $.extend(defaults, settings);
		settings.element = $(this);
		//set opacity to start
		$(settings.element).css("opacity",settings.start_opacity);
		//mouse over
		$(settings.element).hover(
			//mouse in
			function () {                                                  

				$(this).stop().animate({opacity: settings.high_opacity}, settings.timing); //100% opacity for hovered object

				$(this).siblings().stop().animate({opacity: settings.low_opacity}, settings.timing); //dimmed opacity for other objects
			},
			//mouse out
			function () {
				$(this).stop().animate({opacity: settings.start_opacity}, settings.timing); //return hovered object to start opacity

				$(this).siblings().stop().animate({opacity: settings.start_opacity}, settings.timing); // return other objects to start opacity
			}
		);
		return this;
	}
})(jQuery, document);

function init_jfade()
{
	$('.jfade_image').jfade();
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

var latest_news_cont_length=0;
var right_content={};
var music_player_stopped = 0;
var home_fullscreen_interval;
var cur_home_slide =0;
/*function attach_videos()
{
	// Vplayer Parameters
	var param = {
		'preload': true,
		'autoplay': false,
		'download': false,
		'fullscreen': false
	};
	// Creates a new instance of the player
	if($('video').length > 0)
	{
		var trailervideo = new Vplayer('myvideo1', param);
		var trailervideo = new Vplayer('myvideo2', param);
		var trailervideo = new Vplayer('myvideo3', param);	
	}

}*/

function getUrlVars(url)
{
	var vars = {}, hash;
	var hashes = (url.split('?').length>1)?url.split('?')[1].split('&'):'';
	for(var i = 0; i < hashes.length; i++)
	{
		hash = hashes[i].split('=');
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function removeNL(s){ 
	r = "";
	for (i=0; i < s.length; i++) {
	if (s.charAt(i) != '\n' &&
	s.charAt(i) != '\r' &&
	s.charAt(i) != '\t') {
	r += s.charAt(i);
	}
	}
	return r;
}


function article_call()
{
	$.ajax({
		url:'index.php',
		data:getUrlVars(cur_link),
		success: function( data ) {
			var html = ''; 
			html = removeNL(data);
			html = data.split('<div class="right">')[1];
			html = html.split('</div><div class="right_end">')[0];
			right_content.html(html);
			fn_window_resize();
		}
	});
}
function press_call()
{
	$.ajax({
		url:'index.php',
		data:getUrlVars(cur_link),
		success: function( data ) {
			var html = ''; 
			html = removeNL(data);
			html = data.split('<div class="right">')[1];
			html = html.split('</div><div class="right_end">')[0];
			right_content.html(html);
			fn_window_resize();
		}
	});
}
function gallery_call()
{
	$.ajax({
		url:'index.php',
		data:getUrlVars(cur_link),
		success: function( data ) {
			$('#fullscreen-loader').fadeOut(function(){
				right_content.fadeIn();	
			});
			var html = ''; 
			html = removeNL(data);
			html = data.split('<div class="right">')[1];
			html = html.split('</div><div class="right_end">')[0];
			right_content.html(html);
			fn_window_resize();
		}
	});
}

function ajax_call()
{
	cur_link = $(this).attr('ajaxcall');
	
	//if there is no link no need to proceed further
	if(cur_link == 'javascript:void(0);')
	{
		return false;
	}
	
	$('a.activemenu').removeClass("activemenu");
	
	
	var target = (cur_link.split('t=')[1])?cur_link.split('t=')[1].split('&')[0]:'index';
	right_content = $('.content .right');
	
	clearInterval(home_fullscreen_interval);
	
	if($(this).parent().parent().parent().is('.main_menu'))
	{
		$(this).addClass("activemenu");
	}
	
	//root-menu of top menu clicked
	if($(this).parent().parent().parent().is('.main_menu'))
	{
		$(this).addClass("activemenu");
	}
	
	//sub-menu of top menu clicked
	if($(this).parent().parent().parent().is('.main_menu_sub'))
	{
		$(this).parent().parent().parent().parent().parent().children(0).addClass("activemenu");
	}
	
	
	if(target == 'index')
	{
		
		$('a[ajaxcall*="'+cur_link+'"]').addClass("activemenu");
		
		cur_home_slide =0;
		
		$.backstretch(slides[cur_home_slide].image,{speed:500});
		var _link = $('.fullscreen_link a');
		if(slides[cur_home_slide].url != "" && slides[cur_home_slide].url != null)
		{
			_link.fadeIn();
			
			_link.attr('href','javascript:void(0);').attr('ajaxcall',slides[cur_home_slide].url).click(ajax_call);

		}
		else
		{
			_link.fadeOut();
		}
		
		if(right_content.is(':visible'))
		{
			right_content.hide('slow').html('');	
		}
		$(".fullscreen_controls").fadeIn();
		fn_window_resize();
	}
	else if(target == 'article')
	{
		$(".fullscreen_controls").fadeOut();
		$('#fullscreen-loader').fadeIn();
		
		if(right_content.is(':visible'))
		{
			right_content.hide('slow',function(){
				$('#fullscreen-loader').fadeIn('normal',article_call);
			});	
		}
		else
		{
			$('#fullscreen-loader').fadeIn('normal',article_call);
		}
	}
	else if(target == 'contact')
	{
		$('a[ajaxcall*="t=contact"]').addClass("activemenu");
		
		$(".fullscreen_controls").fadeOut();
		$('#fullscreen-loader').fadeIn();
		
		if(right_content.is(':visible'))
		{
			right_content.hide('slow',function(){
				$('#fullscreen-loader').fadeIn('normal',article_call);
			});
		}
		else
		{
			$('#fullscreen-loader').fadeIn('normal',article_call);
		}
	}
	else if(target == 'news')
	{
		$(".fullscreen_controls").fadeOut();
		$('#fullscreen-loader').fadeIn();
		
		if(right_content.is(':visible'))
		{
			right_content.hide('slow',function(){
				$('#fullscreen-loader').fadeIn('normal',article_call);
			});
		}
		else
		{
			$('#fullscreen-loader').fadeIn('normal',article_call);
		}
	}
	else if(target == 'links')
	{
		$(".fullscreen_controls").fadeOut();
		$('#fullscreen-loader').fadeIn();
		
		if(right_content.is(':visible'))
		{
			right_content.hide('slow',function(){
				$('#fullscreen-loader').fadeIn('normal',article_call);
			});
		}
		else
		{
			$('#fullscreen-loader').fadeIn('normal',article_call);
		}
	}
	else if(target == 'gallery')
	{
		$(".fullscreen_controls").fadeOut();
		$('#fullscreen-loader').fadeIn();
		
		if(right_content.is(':visible'))
		{
			right_content.hide('slow',function(){
				$('#fullscreen-loader').fadeIn('normal',gallery_call);
			});
		}
		else
		{
			$('#fullscreen-loader').fadeIn('normal',gallery_call);
		}
	}
	else if(target == 'press')
	{
		$(".fullscreen_controls").fadeOut();
		$('#fullscreen-loader').fadeIn();
		
		if(right_content.is(':visible'))
		{
			right_content.hide('slow',function(){
				$('#fullscreen-loader').fadeIn('normal',press_call);
			});
		}
		else
		{
			$('#fullscreen-loader').fadeIn('normal',press_call);
		}
	}
}

function convert_links()
{
	$("a:not('.no_ajax')").each(function(){
		var link = $(this).attr("href");
		var target =  $(this).attr('target');
		var void_link = 'javascript:void(0);';
		if(link != void_link && target != '_blank')
		{
			$(this).attr('ajaxcall',link);
			$(this).attr('href',void_link);
			$(this).click(ajax_call);
		}
	});
}

function init_mp3player()
{
	$(".mp3player a").click(function(){
		if($(this).is('.disabled'))
		{
			$(this).removeClass("disabled");
			$("#jquery_jplayer_1").jPlayer("play");
			music_player_stopped = 0;
		}
		else
		{
			$(this).addClass("disabled");
			$("#jquery_jplayer_1").jPlayer("pause");
			music_player_stopped = 1;
		}
	});
	
	$("#jquery_jplayer_1").jPlayer({
		ready: function () {
			$(this).jPlayer("setMedia", {
				mp3:mp3_file
			}).jPlayer("play");
		},
		supplied:'mp3',
		solution: 'flash,html',
		ended: function (event) {
			$(this).jPlayer("play");
		},
		swfPath: "view/js",
		supplied: "mp3"
	});
}

function rotatefullscreen()
{
	$('.fullscreen_next').click();
}

function home_fullscreen()
{

	$('.fullscreen_link a').attr('href','javascript:void(0);').attr('ajaxcall',slides[cur_home_slide].url).click(ajax_call);
	cur_home_slide = 0;
	$(".fullscreen_next").click(function(){
		cur_home_slide++;
		if(cur_home_slide == slides.length)
		{
			cur_home_slide=0;
		}
		var link = $('.fullscreen_link a');
		
		if(slides[cur_home_slide].url != ""  && slides[cur_home_slide].url != null)
		{
			link.fadeIn();
			link.attr('href','javascript:void(0);').attr('ajaxcall',slides[cur_home_slide].url).click(ajax_call);
		}
		else
		{
			link.fadeOut();
		}
		$.backstretch(slides[cur_home_slide].image,{speed:500});
	});
	$(".fullscreen_prev").click(function(){
		cur_home_slide--;

		if(cur_home_slide < 0)
		{
			cur_home_slide=slides.length-1;
		}

		var link = $('.fullscreen_link a');
		
		if(slides[cur_home_slide].url != "" && slides[cur_home_slide].url != null)
		{
			link.fadeIn();
			link.attr('href','javascript:void(0);').attr('ajaxcall',slides[cur_home_slide].url).click(ajax_call);
		}
		else
		{
			link.fadeOut();
		}
		$.backstretch(slides[cur_home_slide].image,{speed:500});
		
	});
}

function fn_window_resize()
{
	var window_height = $(window).height();
	var window_width = $(window).width();
	var document_height =$(document).height();
	var document_width = $(document).width();
	var biggest_height =  Math.max(window_height,document_height);
	var window_scrolltop = $(window).scrollTop();

	//body
	if((window_height > 700 && window_width > 1100) || $(".bottom_gallery_thumbs").length > 0)
	{
		$(window).scrollTop(0);
		$("body").css("overflow","hidden");
		$(".bottom_wrapper").css("position","fixed").css("float","none");
		$(".bottom_menu_wrapper").css("position","fixed").css("bottom","55px").css("float","none").css("margin-bottom","0px");
		
	}
	else if($(".bottom_gallery_thumbs").length == 0)
	{
		$(".bottom_wrapper").css("position","relative").css("float","left");
		$("body").css("overflow","auto");
		/*$( ".bottom_menu_wrapper" ).position({
			my:'top',
			at:'top',
			of:$(".bottom_wrapper")
		});*/
		$(".bottom_menu_wrapper").css("position","relative").css("bottom","30px").css("margin-bottom","-30px").css("float","left");
	}
	

	
	//wrapper
	/*$(".bottom_wrapper , .bottom_menu_wrapper ").css('top',window_height+window_scrolltop);*/
	
	//left_wrapper , right_wrapper
	$(".left_wrapper , .right_wrapper").css('height',window_height-55);
	
	
	//content_wrapper
	if(window_width < 1100)
	{
		$(".left").css('padding-left','45px');
		$(".content").css('width','1100px');
		$(".right").css('width','752px');
	}
	else
	{
		$(".left").css('padding-left','0px');
		$(".content").css('width','980px');
		$(".right").css('width','737px');
	}
	
	//if for the gallery page
	if($(".bottom_gallery_thumbs").length > 0)
	{
		if(window_width < 1100)
		{
			$(".right").css('padding-left','45px').css('position','absolute');
			$("body").css('overflow-y','hidden').css('overflow-x','auto');
		}
		else
		{
			$(".right").css('padding-left','0px').css('position','fixed');
			$("body").css('overflow','hidden');
		}
		$(".right").css('height','inherit').css('width','970px');
	}
	else
	{
		$(".right").css('height',biggest_height-110).css('padding-left','0px').css('position','static');
	}
	
	$(".main_menu_off").css('height',window_height*0.75);
}

$(function(){
	right_content = $('.content .right');
	$(".main_menu > ul > li > a").hover(function(){
		$(".main_menu > ul > li > a.selected").not($(this)).removeClass('selected');
		$(".main_menu > ul > li > .main_menu_sub_wrapper").not($(this).next()).fadeOut();
		$(this).addClass('selected');
		$(this).next().fadeIn();
		$(".main_menu_off").css('display','block');
	},function(){
	});
	
	$(".main_menu_off").hover(function(){
		$(".main_menu_off").css('display','none');
		$(".main_menu > ul > li > a.selected").removeClass('selected');
		$(".main_menu > ul > li > .main_menu_sub_wrapper:visible").fadeOut();
	},function(){

	});
	
	$("#accordion").accordion({
		collapsible: true,
		autoHeight:false,
		active:false
	});

	
	$('.must_see_header').click(function(){
		if($(".must_see").css('top') == '-340px')
		{
			$(".must_see_slider").hide(0);
				
			$('.must_see_header').removeClass('must_see_header_close');
			$('.must_see').animate({
				top:'0px'
			});
			
			$(".must_see_slider > div:visible").hide('fast',function(){
				$(this).show('fast');
			});	
			
			if(music_player_stopped == 0)
			{		
				$('.mp3player a').removeClass("disabled");
				$("#jquery_jplayer_1").jPlayer("play");
			}
		}
		else
		{
			$(".must_see_slider").show(0);
			$('.must_see_header').addClass('must_see_header_close');
			$('.mp3player a').addClass("disabled");
			$("#jquery_jplayer_1").jPlayer("pause");
			
			$('.must_see').animate({
				top:'-340px'
			});
		}
	});
	
	$(".lang_item").hover(function(){
		$(this).children(".lang_sqr").show(200);
	},function(){
		$(this).children(".lang_sqr").hide(200);
	});
	
	$(".bottom_menu_right_btn_head a").click(function(){
		$(".bottom_menu_right_btn_head a.selected").each(function(){
			var box = $(this).parent().parent().children(".bottom_menu_right_btn_box");
			$(this).removeClass('selected');
			box.animate({
				bottom:'-'+box.height()+'px'
			});
		});
		var box = $(this).parent().parent().children(".bottom_menu_right_btn_box");
		if(box.css('bottom') == '30px')
		{
			$(this).removeClass('selected');
			box.animate({
				bottom:'-'+box.height()+'px'
			});
		}
		else
		{
			$(this).addClass('selected');
			box.animate({
				bottom:'30px'
			});
		}

	});
	
	$(".must_see_slider").cycle({
		timeout: 0, 
		pager:  '.must_see_nav', 
		before:function(){
		},
		// callback fn that creates a thumbnail to use as pager anchor 
		pagerAnchorBuilder: function(idx, slide) { 
			
			return '<a goto="'+idx+'" href="javascript:void(0);" style="background-image:url(\''+$(slide).attr('thumb')+'\')"></a>'; 
		} 
	});
	
	
	$(".must_see_desc > div:first").show(0);
	$(".must_see_desc > div:gt(0)").hide(0);
	$(".must_see_slider").hide(0);
	$(".must_see_nav a").click(function(){
		var goto = $(this).attr("goto");
		$(".must_see_desc > div:visible").hide(0);
		$(".must_see_desc > div:eq("+goto+")").fadeIn();	
	});
	
	
	
	
	latest_news_cont_length = $(".latest_news_cont > div").length;
	$(".latest_news_cont").cycle({
		timeout: 0, 
		fx:'scrollHorz',
		speed:400,
		next:'.latest_news_next',
		prev:'.latest_news_prev',
		nowrap:true,
		onPrevNextEvent:function(isNext, zeroBasedSlideIndex, slideElement) {
			if(zeroBasedSlideIndex==0)
			{
				$('.latest_news_prev').hide();
			}
			else
			{
				$('.latest_news_prev').show();
			}
			if(zeroBasedSlideIndex+1== latest_news_cont_length)
			{
				$('.latest_news_next').hide();
			}
			else
			{
				$('.latest_news_next').show();
			}
		}
	});
	
	
	
	$('.newsletter_field input').focus(function(){
		if($(this).val() == $(this).attr('default'))
		{
			$(this).val('');
		}
	});
	$('.newsletter_field input').blur(function(){
		if($(this).val() == $(this).attr('default'))
		{
			$(this).val($(this).attr('default'));
		}
	});
	
	$('.newsletter_btn a').click(function(){
		var email = $(".newsletter_field input").val();
		var email_input = $(".newsletter_field input");
		if(isValidEmailAddress(email) == true)
		{
			email_input.css("border-color","#414141");
			$(".newsletter_field , .newsletter_btn").css("display","none");
			$(".newsletter_loading").css("display","block");
			$.get("index.php",{email:email,t:'newsletter.add'},function(data){
				$(".newsletter_loading").hide();
				$(".newsletter_success").show();
			}); 
		}
		else
		{
			email_input.css("border-color","#FCB100");
		}
		return false;
	});
	
	init_jfade();
	
	convert_links();
	
	init_mp3player();
	
	home_fullscreen();
	
	$(window).resize(fn_window_resize);
	$(window).resize();
	fn_window_resize();
	
});
