jQuery(function (){
	initialize();
});

function initialize() {
	animate = false;
	
	jQuery('.pages').find('a').click(function(){
		changeImg(jQuery(this).attr('id'));
		return false;
	});
	
	jQuery('.screen').find('a').click(function(){
		if(jQuery(this).next().attr('href')){
			var name = jQuery(this).next().attr('id').substring(3);
			changeImg(name);
		} else {
			changeImg(1);	
		}
		return false;
	});
	
	jQuery('.tabs').find('a').click(function(){
		changeTab(jQuery(this).attr('id'));
		scrollToID('title');
		
		return false;
	});	
	
	jQuery('.videoCategories').find('a').click(function(){
		resizeEXT();
	});
}

function changeTab(id){
	jQuery('.tabs').find('a').removeClass();
	jQuery('.tabs').find('#'+id).removeClass().addClass('selected');
	jQuery('#content').find('.tabshow').removeClass().addClass('tabhidden');
	jQuery('#cat'+id).removeClass().addClass('tabshow');
}

function changeImg(idA) {
	var currentID = jQuery('.tabshow').find('.screen').find('.show').attr('id');
	var nextID = 'img' + jQuery('#' + idA).attr('id');
	
	if(nextID == currentID){
		return false;	
	}
	
	scrollToID('title');

	jQuery('.tabshow').find('.pages').find('a').removeClass();
	jQuery('.tabshow').find('#' + idA).addClass('current');
	
	jQuery('.tabshow').find('.screen').css({
		'width': jQuery('.tabshow').find('.screen').width(),
		'height': jQuery('.tabshow').find('.screen').height()
	});
	var imgPos = jQuery('.tabshow').find('.screen').find('.show').position();
	var imgWidth = jQuery('.tabshow').find('.screen').find('.show').width();
	var imgHeight = jQuery('.tabshow').find('.screen').find('.show').height();
	
	var screenHeight = jQuery('.tabshow').find('.screen').height();
	
	/*jQuery('.screen').find('.show').css({
		'z-index': 1
	});*/
	jQuery('.tabshow').find('#screenEXT_lft').css({
		'height': screenHeight+30
	});
	
	jQuery('.tabshow').find('#screenEXT_rgt').css({
		'height': screenHeight+30
	});
	jQuery('.tabshow').find('#' + nextID).css({
		'position': 'absolute',
		'opacity': 1,
		'top': imgPos.top,
		'left': imgPos.left,
		'width': imgWidth,
		'height': imgHeight,
		'z-index': -1
	});
	jQuery('.tabshow').find('#' + nextID).removeClass().addClass('show');
	jQuery('.tabshow').find('#' + currentID).removeClass().addClass('hidden');
	
	return false;
}

function resizeEXT(){
	var imgHeight = jQuery('.tabshow').find('.screen').find('.show').height();
	
	jQuery('#screenEXT_lft').css({
		'height': imgHeight
	});
}

function scrollToID(id){
	if(animate!=true){
		speed = 100;
		animate = true;
		
		jQuery(this).removeAttr('href');
		var targetOffset = jQuery('#' + id).offset().top;
		jQuery('html,body').animate({scrollTop: targetOffset}, speed, function() {
			animate = false;	
		});
	}
}
function createPlayer(div,file) {
	var s = new SWFObject('videos/MB-player.swf?' + Math.round(1000 * Math.random()),'thePlayerId','631','355','7');
	s.addParam("allowfullscreen","true");
	s.addVariable("skin","videos/modieus.swf");
	s.addVariable("controlbar","over");
	s.addVariable("file",file);
	s.addVariable("width","631");
	s.addVariable("height","355");
	s.addVariable("displayheight","355");
	s.addVariable("autostart","true");
	s.addVariable("overstretch","uniform");
	s.addVariable("enablejs","true");
	s.addVariable("javascriptid","thePlayerId"); 
	s.write(div);
}

