// JavaScript Document
$(document).ready(function(){
	
	creratePopup();
	addListeners();	 
	applyVideo('http://www.youtube.com/embed/1rgojivXD0g')
	//appear(true);
						   
});

function creratePopup()
{
 
  var HTML_CONTENT="<div id='webguru_video_popup_box'><div class='vid_pop_up_bg'></div><div class='youtubeContainer'></div><div class='crossBt'></div></div>";
  $('body').append(HTML_CONTENT);  
  resizeVidPopUp();
  $(window).resize(resizeVidPopUp);
  $('#webguru_video_popup_box .crossBt').bind('click',function(){$('#webguru_video_popup_box').hide(); })
}

function resizeVidPopUp()
{
  var W = $(window).width();
  var H = $(window).height();
  var wa = $('#webguru_video_popup_box .youtubeContainer').css('width');
  var ha = $('#webguru_video_popup_box .youtubeContainer').css('height');
  
  var leftAmount = (W - parseInt(wa))/2;
   var topAmount = (H - parseInt(ha))/2;
  
  $('#webguru_video_popup_box .vid_pop_up_bg').css('opacity',0.8);
  $('#webguru_video_popup_box .vid_pop_up_bg').css('width',W+'px');
  $('#webguru_video_popup_box .vid_pop_up_bg').css('height',H+'px');
  
  $('#webguru_video_popup_box .youtubeContainer').css('left',leftAmount);
  $('#webguru_video_popup_box .youtubeContainer').css('top',topAmount);
  $('#webguru_video_popup_box .youtubeContainer').css('box-shadow','10px 10px 20px #000');
  $('#webguru_video_popup_box .youtubeContainer').css('-webkit-box-shadow','0px 0px 20px #000');
  $('#webguru_video_popup_box .youtubeContainer').css('-moz-box-shadow','10px 10px 20px #000');
  
  $('#webguru_video_popup_box .crossBt').css('left',(parseInt($('#webguru_video_popup_box .youtubeContainer').css('left'))+parseInt(wa)-0)+'px');
  $('#webguru_video_popup_box .crossBt').css('top', (parseInt($('#webguru_video_popup_box .youtubeContainer').css('top'))-10)+'px');
 
}

function applyVideo(path)
{
  	var CONTENT = '<iframe title="YouTube video player" width="640" height="390" src="'+path+'?hd=1" frameborder="0" allowfullscreen></iframe>'
	$('#webguru_video_popup_box .youtubeContainer').html(CONTENT);
}

function appear(bool)
{
   if(bool)	
   {
	 $('#webguru_video_popup_box').fadeIn('slow');  
   }
   else
   {
	 $('#webguru_video_popup_box').hide('fast');    
   }
   
}

function addListeners()
{
	$('#vidContainer').find('img').bind('click',function(){
														 		if($(this).attr('rel'))
																{
														 			applyVideo($(this).attr('rel'));
																	appear(true);
																	
																}
														   });
	
}
