
var layout = {

	resize: function()
	{
		var page_height = $('#page').height();
		var page_width = $('#page').width();
		var doc_height = $(document).height();
		
		bar_height = (page_height < doc_height) ? page_height : doc_height;
		
		$('.side_bars, .side_bar, .side_bar .content, .side_bar .contentInner, .handles').height(bar_height);
		
		// odd display bug in IE 7. Explicitly set the width and height after the page loads
		// to force a redraw, otherwise the Flash won't appear unless the window is scaled.
		$('#flash_wrapper').css({width: '100%', height: '100%'});

		if(page_width < 996) 
		{
			$('body').addClass('narrow');
		}
		else
		{
			$('body').removeClass('narrow');
		}
		
		if( $('body#pledge').length > 0 )
		{
			this.resize_pledge();
		}
	},
	
	resize_pledge: function()
	{
		var footer_height = $('#footer').height();
		var page_height = $('#page').height();
		var page_width = $('#page').width();
		var panel_height = $('.bottom_panel').height();
		var new_height = page_height - (footer_height + panel_height) + 30;
		
		$('#flash_wrapper').css({height: new_height , minHeight: 0, width: page_width});
	},
	
	get_bar_height: function()
	{
		return $('.#side_bar').height();
	}
}

var flash = {
	
	get_movie: function(movie_name) {   
	    //var isIE = navigator.appName.indexOf("Microsoft") != -1;   
	    //return (isIE) ? window[movie_name] : document[movie_name];  
	
		return document.getElementById(movie_name);
	}
	
}

var sound = {
	
	on: function()
	{
		flash.get_movie("flash").soundOn();
		$('.sound a').siblings('.icon_sound').removeClass('off');
		$('.sound #sound_toggle_on').addClass('active');
		$('.sound #sound_toggle_off').removeClass('active');
	},
	
	off: function()
	{
		flash.get_movie("flash").soundOff();
		$('.sound a').siblings('.icon_sound').addClass('off');
		$('.sound #sound_toggle_off').addClass('active');
		$('.sound #sound_toggle_on').removeClass('active');
	}
	
}


var side_bar = {
	
	speed: 500,
	ease: 'easein',
	width: '260',

	show: function(ele)
	{		
		var content = ele.children('.content');
		
		if(content.hasClass('inactive'))
		{
			this.hide_all();
			
			if($.browser.msie && $.browser.version < 7)
			{
				content.css('width', this.width + 'px').show();
			}
			else
			{
				content.css('width', 0).animate({
					width: this.width + 'px'
				}, this.speed, this.ease, function(){ content.removeClass('inactive') });
			}
		} 
	},
	
	hide: function(ele)
	{
		var content = ele.children('.content');
		
		if($.browser.msie && $.browser.version < 7)
		{
			content.css('width', 0).hide();
		}
		else
		{
			content.animate({
				width: 'hide'
			}, this.speed, this.ease, function(){ content.addClass('inactive') });
		}
	},
	
	hide_all: function()
	{
		var content = $('.side_bar').children('.content');
		
		if($.browser.msie && $.browser.version < 7)
		{
			content.css('width', 0).hide();
		}
		else
		{
			content.animate({
				width: 'hide'
			}, this.speed, this.ease, function(){ content.addClass('inactive') });
		}
	}
	
}


$(function(){
	
	layout.resize();
	$(window).resize(function(){
		layout.resize();
	});
	
	$('.video_links .video_link').bigClick();
	
	$('.video_link a').each(function(){
		var file = $(this).attr('rel');
		
		$(this).parents('div').eq(0).addClass('clickable');
		
		if(file)
		{
			$(this).parents('div').eq(0).click(function(){
				flash.get_movie("flash").chooseVideo(file);
			});
		}	
	});
	
	$('#nav_main li').prepend('<div class="hover"></div>');
	$('#nav_main li').hover(function(){
		$(this).children('div.hover').animate({
			height: '30px'
		}, 250, 'easein');
	}, function(){
		$(this).not('.active').children('div.hover').animate({
			height: '0px'
		}, 250, 'easein', function(){ $(this).hide() });
	});
	
	$('.handles .handle').hover(function(){
		var id = $(this).attr('id');
		side_bar.show( $('#side_bar_'+id) );
	});
	
	$('.side_bars').mouseleave(function(){
		side_bar.hide( $(this).children('.side_bar:visible') )
	});
	
	$('.sound a').click(function(){
		var action = $(this).attr('rel');
		
		if(action == 'on') {
			sound.on();
		} else {
			sound.off();
		}
		
		return false;
	});
	
	$("input[type=file]").filestyle({ 
		image: baseUrl + "/assets/images/design/browse_avatar_file.gif",
		imageheight : 119,
		imagewidth : 119,
		width : 114,
		position: '',
		css: { float: 'left', cursor: 'pointer' }
	 });

	$('body#downloads .list_download a').hover(function(){
		var img = $(this).attr('rel');
		$('body#downloads .preview_download').children('img').hide();
		$('body#downloads .preview_download').children('.'+img).show();
	}, function(){
		$('body#downloads .preview_download').children('img').hide();
	});

});

jQuery.fn.bigClick = function(options)
{
	var settings = {
		cssClass : 'clickable'
    }
	
	if(options) {
        jQuery.extend(settings, options);
    };
	
	this.each(function(){
		var anchor = $(this).find('a')[0];
		
		if(!anchor)
			return;			
		
		var href = $(anchor).attr('href');
		var title = $(anchor).attr('title');
		
		$(this).attr('title', title);
		
		$(this).addClass(settings.cssClass).click(function(){
			window.location = href;
		});
	});
}
