// VERTICALLY ALIGN FUNCTION
(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('padding-top', mh);
	});
};
})(jQuery);


var ieFix=0;
var ieVersion=0;
var selectorPreload='body';	
var preloader;
var mainpagetest;

$(document).ready(function(){
						   
	var ie = navigator.userAgent.match(/MSIE (\d+(?:\.\d+)+(?:b\d*)?)/);
	
	if (ie && ie[0].match("MSIE")) {
		ieFix=1; 
		ieVersion=Math.ceil(ie[1]);
	} else ieFix=0;
	
	NavigationCallback = onPageLoad;
	NavigationPreCallback = onPagePreLoad;
	onPageLoad('', AjaxPageContent);
	
	AjaxInit();

	preloader = $("<div></div>").appendTo(selectorPreload);
	$(preloader).css({
		height: 	"0px",
		width:		"0px",
		overflow:	"hidden"
	});

	DomCorners('.cbox', 'form/gro/i/main-corner.png',13);	
	$('.frame').vAlign();
	
	if (!ieFix || (ieFix && ieVersion>=7)) background_images_rotate('background');
	
	$('.mcontrol .switch_lang').click( function() {
		$('.prefix_ru').toggle('blind');
		$('.prefix_en').toggle('blind');
		return false;
	});

	setupAjaxLink('#menu');
	setupAjaxLink('#footermenu');
	setupAjaxLink(AjaxPageContent);
	
	$('.showplayer a.switchplayer').click( function () {		
		if ($('#videoplayer').css('display')!='none') $('#GROModule').hide();
		$('#videoplayer').slideToggle(800, function () {
													 
			if ($('#videoplayer').css('display')=='none') $.scrollTo('#ajaxPageContentTop', { duration:700, offset:-180 });
			else {
				$('#GROModule').show();
				$.scrollTo('#videoplayerTop', { duration:700, offset:-180 });
			}

		});
		return false;
	});
	
	$('.radiolist ul a').click( function() {
		var def=$(this).attr('rel');
		loadPlayList(albumplayer_top, def, 1);
		return false;
	});
	
});

function onPagePreLoad(url, rel) {
	if (rel==AjaxPageContent) {
		mainpagetest = new RegExp('main','i');
		// Load main content - HIDE video player
		if ((!url && fpage!='main') || (url && !mainpagetest.test(url))) {
			if ($('#videoplayer').css('display')!='none') {
				$('#GROModule').hide();
				$('#videoplayer').slideUp(800, function () {
					$.scrollTo('#ajaxPageContentTop', { duration:700, offset:-180 });
				});
			}
		}
	}
}

function onPageLoad(url, rel) {
	
	if (rel==AjaxPageContent) {

		if (url) {
			// Menu selection	
			var p = strrpos(url, '~');
			if (p) urltest=url.substr(0,p);	else urltest=url;
			
			$('.mainmenu li').removeClass('select');
			$('.mainmenu li a[href^="'+urltest+'"]').each( function (){
				$(this).parent().addClass('select');
			});
		}

		// Calendar
		$('.calendar-link').click( function (){
			var rel=$(this).attr('rel');
			$(rel).slideToggle('slow');
			return false;
		});
	
		$('.calendar-content .scrollable').jScrollPane({
			scrollbarWidth:7, scrollbarMargin:5, showArrows:false, wheelSpeed:40
		});
		
		$('.calendar-content').slideToggle('slow');
		
			
		// ------------------------------------------
		// Photo gallery OVERLAY
		var imglinks = $(".photo-gallery a");
		if (imglinks.length)
		$(".photo-gallery a").overlay({ 
			target: '#gallery',
			expose: { color: '#ffffff', 	loadSpeed: 200, 	opacity: 0.5 },
			onBeforeLoad: function () {
				$('#gallery #img').attr('src','');
				$('#gallery #img').hide();
			}
		}).gallery({ 
			speed: 900,
			autohide: false, 
			template: '${url}<p><b>${title}</b></p>'
		});	

		// ------------------------------------------
		// Video gallery OVERLAY
		var videolinks = $(".video-gallery a");
		if (videolinks.length)
		$(".video-gallery a").overlay({ 
			target: '#popupvideo',
			expose: { color: '#ffffff', 	loadSpeed: 200, 	opacity: 0.5 },
			onBeforeLoad: function () {
				controlInteractive('pause');
				var loader = this.getOverlay().find(".progress");
				loader.show();
				var wrap = this.getOverlay().find(".contentWrap");
				var rev = this.getTrigger().attr("rev");
				var rel = this.getTrigger().attr("rel");
				wrap.html( embed_video( rev, rel ) );
			},
			onLoad: function () {
				var loader = this.getOverlay().find(".progress");
				loader.hide();
			},
			onClose: function () {
				$('#popupvideo .contentWrap').html('');
				controlInteractive('play');
			}
		});	

		// ------------------------------------------
		// Music gallery OVERLAY
		var musiclinks = $(".music-gallery a");
		if (musiclinks.length)
		$(".music-gallery a[rev]").overlay({ 
			target: '#popupvideo',
			expose: { color: '#ffffff', 	loadSpeed: 200, 	opacity: 0.5 },
			onBeforeLoad: function () {	
				controlInteractive('pause');
				var loader = this.getOverlay().find(".progress");
				loader.show();
				var wrap = this.getOverlay().find(".contentWrap");
				var rev = this.getTrigger().attr("rev");
				var rel = this.getTrigger().attr("rel");
				wrap.html( embed_music( rev, rel ) );				
			},
			onLoad: function () {
				var loader = this.getOverlay().find(".progress");
				loader.hide();
			},
			onClose: function () {				
				$('#popupvideo .contentWrap').html('');
				controlInteractive('play');
			}
		});	

		// Show-hide tracks
		$('.music-gallery .more,.music-gallery .i[rel]').click(function() {

			var id = $(this).attr('rel');			
			switchMusicBox(id, 'toggle');
												 
			return false;
		});
		
		// Album switch
		$('.musicg .icover').click ( function() {
			var boxid=$(this).attr('rel');
			var parentboxid=$(this).attr('rev');

			if (!$(this).hasClass('active')) {
				$(parentboxid+' .icover').removeClass('active');
				$(this).addClass('active');
				$(parentboxid+' .album:visible').slideUp(600, function () {
					$(boxid).slideDown(700, function() {
						switchMusicBox(parentboxid, 'open');													 
					});
				});
				
			}
			return false;
		});
		
		
		// Projects - switch tabs
		$('.projects .control li').click( function () {
			var id=$(this).attr('rel');
			var parentid=$(this).attr('rev');

			$(parentid+' .control li').removeClass('active');
			$(this).addClass('active');
			$(parentid+' .tab').hide();
			$(parentid+' '+id).show();
			
			if (! $(parentid+' '+id+' .jScrollPaneContainer').size()) {
				//$('.projects .tab .scrolled:visible').jScrollPane({
				$(parentid+' '+id+' .scrolled').jScrollPane({
					scrollbarWidth:18, scrollbarMargin:5, showArrows:true, wheelSpeed:40
				});
			}
			
			return false;
		} );
		
		$('.projects .tab .scrolled:visible').jScrollPane({
		//$(parentid+' '+id+' .scrolled').jScrollPane({
			scrollbarWidth:18, scrollbarMargin:5, showArrows:true, wheelSpeed:40
		});
		
	}

}

function switchMusicBox (id, type) {
	var h=$(id+' .wrapper').height();			
	//var nh=$('.musicg li').css('height');
	var nh='110px';
	
	if ($(id).css('height')!=nh && type != 'open') {
		h=nh; $('.music-gallery .i[rel="'+id+'"]').removeClass('active');
	} else
		$('.music-gallery .i[rel="'+id+'"]').addClass('active');

	if (h==nh && type != 'open') $(id+' .moremore').fadeIn(400);
	else $(id+' .moremore').fadeOut(400);

	$(id).animate({height: h}, 900, '', function () {
	});	
}

function setImages ( prefix, arr, time ) {

	background_images[prefix]=arr;
	background_images_index[prefix]=0;
	background_images_count[prefix]=background_images[prefix].length;
	background_switchtime[prefix]=time;
	
	background_images_rotate(prefix);
}

function background_images_rotate_control ( type ) {
	
	if (ieFix && ieVersion<7) return;
	
	$.each(background_images_index, function (i,v) {
		if (type=='pause') clearTimeout(background_timeout[i]);
		else if (type=='play') {
			if (background_switchtime[i])
				background_timeout[i]=setTimeout("background_images_rotate('"+i+"')", background_switchtime[i]);			
		}
	});
}

// -------------------------------------------------------------------------------------------------------------
function background_images_rotate( i ) {	
//	if ( i == 'background' && groundTypes[groundActive] == 'video' ) return;
	var id='#id'+i;
	
	if (ieFix && ieVersion<7) return;

	clearTimeout(background_timeout[i]);
	
	// Load background_images_index
	var image=background_images[i][background_images_index[i]];
	
	if (background_loaded[i]) {
		
		if (ieFix) {
			
			if (id=='#idtask') {
				h = parseInt($(id).css('height')); 
				image = "_mod/_personal/imgresize.php?size_height="+h+"&img="+image;
				$(id).css('background','url('+image+') top center no-repeat');
			} else $(id).css('background','url('+image+') center');
			
		} else
		
		$(id).fadeOut( 700, function() {													  
			if (id=='#idtask') {
				h = parseInt($(id).css('height')); 
				image = "_mod/_personal/imgresize.php?size_height="+h+"&img="+image;
				$(id).css('background','url('+image+') top center no-repeat');
			} else $(id).css('background','url('+image+') center');
			$(id).fadeIn( 500 );
		});						
		
	} else {	
		var imgLoad = $("<img/>");
		
		if (id=='#idtask') {
			h = parseInt($(id).css('height')); 
			image = "_mod/_personal/imgresize.php?size_height="+h+"&img="+image;
		}
		
		$(imgLoad).attr("src", image);
		$(imgLoad).unbind("load");
		$(imgLoad).bind("load", function() {

			if (ieFix) {
				if (id=='#idtask') $(id).css('background','url('+image+') top center no-repeat');
				else $(id).css('background','url('+image+') center');	
			} else 

			$(id).fadeOut( 700, function() {													  
				if (id=='#idtask') $(id).css('background','url('+image+') top center no-repeat');
				else $(id).css('background','url('+image+') center');	
				$(id).fadeIn( 500 );
			});		
			
		});
		$(imgLoad).appendTo($(preloader));
	}

	// Next image
	background_images_index[i]++;
	if (background_images_index[i] >= background_images_count[i]) {
		background_images_index[i]=0;
		background_loaded[i]=1;
	}

	if (background_switchtime[i])
		background_timeout[i]=setTimeout("background_images_rotate('"+i+"')", background_switchtime[i]);
}


function embed_video ( vars, info ) {
	
var content='';
if (info) content='<div class="info">'+$(info).html()+'</div>';

return '<object width="480" height="400"><param name="movie" value="http://grostudio.com/embed_video.swf"><param name="menu" value="false"></param><param name="allowfullscreen" value="true"></param><param name="bgcolor" value="#000000"></param><param name="flashvars" value="'+vars+'"></param><embed src="http://grostudio.com/embed_video.swf" flashvars="'+vars+'" type="application/x-shockwave-flash" menu="false"  allowfullscreen="true" bgcolor="#000000" width="480" height="400"></embed></object>'+content;

}

function embed_music ( vars, info ) {

var content='';
if (info) content='<div class="info">'+$(info).html()+'</div>';
	
return '<object width="480" height="400"><param name="movie" value="http://grostudio.com/embed_audio.swf"><param name="menu" value="false"></param><param name="allowfullscreen" value="true"></param><param name="bgcolor" value="#000000"></param><param name="flashvars" value="'+vars+'"></param><embed src="http://grostudio.com/embed_audio.swf" flashvars="'+vars+'" type="application/x-shockwave-flash" menu="false" allowfullscreen="true" bgcolor="#000000" width="480" height="400"></embed></object>'+content;

}

function controlInteractive ( type ) {	
	playControl( type );	
	background_images_rotate_control(type);	
}

function onPlayerState ( type ) {
	if (type == 'play') controlInteractive( 'play' ); else controlInteractive( 'pause' );
}
