$(function(){
	
	var $window, $document, $popup, width, height, popup_width, popup_left;
	
	$window = $(window);
	$document = $(document);
	$popup = $('#popup1');
	$overlay = $('.overlay');
	width = $document.width();
	height = $document.height();
	
	
	$('.overlay').width(width).height(height);
	
	$window.resize(function(e){
	
		width = $document.width();
		height = $document.height();
		
		$overlay.width(width).height(height);
		
		popup_left = (($window.width() - popup_width) / 2) + 'px';
		$popup.css('left',popup_left);
	
	});

	$('.modal .btn_close').live('click',function(e){
	
		$popup.fadeOut(500, function(){

			$overlay.fadeOut();
			//$overlay.hide();

		});
		
		e.preventDefault();
	
	});

	$('.items div.panel div.thumbnail').live('mouseenter', function(e){$(this).find('.description').fadeIn();});

	$('.items div.panel div.thumbnail').live('mouseleave', function(e){$(this).find('.description').fadeOut();});

	$('.items div.panel div.thumbnail').live('click', function(e){
		
		var $that = $(this).find('a');
		var href = $that.attr('href');

		if(!$(this).hasClass('end'))
		{

		$overlay.show();
			
		$.ajax({
		
			url: href,
			type: 'GET',
			success: function(msg){
			
				popup_width = $popup.width();
				popup_left = (($window.width() - popup_width) / 2) + 'px';
				$popup.html(msg).css('top','30px').css('left',popup_left);
				
				if($that.hasClass('campaign'))
				{
				
					var $asset = $('#assets .asset a',$popup).eq(0);
					var $href = $asset.attr('href');
					var $media_type = $asset.attr('rel');

					if($media_type == 'Image')
					{
			
						$('#item',$popup).html('<img src="' + $href+ '" />');
		
					}
					else if($media_type == 'Video')
					{loadVideo($href);}
					else if($media_type == 'Flash')
					{loadVideo($href);}
					
				}
				else
				{
				
					$('#item').css('height', '100%');
				
				}
				
				$popup.fadeIn();
			
			}
		
		});

		}

	});

	/*$('.items div.panel div.thumbnail a').live('click', function(e){
			
		var href = $(this).attr('href');
		var $that = $(this);

		$overlay.show();
			
		$.ajax({
		
			url: href,
			type: 'GET',
			success: function(msg){
			
				popup_width = $popup.width();
				popup_left = (($window.width() - popup_width) / 2) + 'px';
				$popup.html(msg).css('top','100px').css('left',popup_left);
				
				if($that.hasClass('campaign'))
				{
				
					var $first = $('#assets .asset a',$popup).eq(0).attr('href');
					$('#item',$popup).html('<img src="' + $first + '" />');
					
				}
				else
				{
				
					$('#item').css('height', '100%');
				
				}
				
				$popup.fadeIn();
			
			}
		
		});

		
		e.preventDefault();

	});*/

	$('div.asset p a',$popup).live('click',function(e){
	
		var $asset = $(this).attr('href');
		var $media_type = $(this).attr('rel');

		if($media_type == 'Image')
		{
			
			$('#item',$popup).html('<img src="' + $asset + '" />');
		
		}
		else if($media_type == 'Video')
		{loadVideo($asset);}
		else if($media_type == 'Flash')
		{loadVideo($asset);}

		e.preventDefault();
	
	});

	$overlay.click(function(e){
			
		$popup.fadeOut(500, function(){
		
			$overlay.fadeOut();
			//$overlay.hide();

		});

	});

	$(document).keyup(function(e) {

  		if (e.keyCode == 27) { 
			
			$popup.fadeOut();
			$overlay.fadeOut();

		 }

	});

	function loadVideo(asset)
	{
		
		$.ajax({
		
			url: asset,
			type: 'GET',
			success: function(msg){
			
				$('#item',$popup).html(msg);
				var $embed = $('#item #mediaspace embed');
				var $item = $('#item');
				var embed_width = $embed.width();
				var embed_height = $embed.height();
				var item_width = $item.width();
				var item_height = $item.height();
				var offset_left = (item_width - embed_width) / 2;
				var offset_top = (item_height - embed_height) / 2;
				$embed.css('margin-left',offset_left + 'px').css('margin-top',offset_top + 'px');
				//console.log(offset_left);
				//console.log(offset_top);
			
			}
		
		});

	}
	
});
