(function($) {
	
	var slideItemWidth = null;
	var slideItem = null;
	var slideInterval = null;
	var rotateTrack = 1;
	var rotateItems = 5;
	var rotateIntervalID = null;
	
	function animateLogoSlide() {
		var currentLeft = parseInt($('#logoSlider .slideItem').css('left'),10);
		if(!currentLeft) currentLeft = 0;
		
		var newLeft = currentLeft - 1;
		if(newLeft < 0-slideItemWidth) newLeft = 0;
		
		$('#logoSlider .slideItem').css('left',newLeft);
	}
	
	function initLogoSlide() {
		// show the left cover
		$('#logoSlider .coverLeft').css('visibility','visible');
		
		// init some vars
		slideItemWidth = $('.slideItem').width();
		slideItem = $('#logoSlider .slideItem').clone();
		
		// duplicate slide item
		$('#logoSlider .slideArea').css('width',slideItemWidth * 2);
		$('#logoSlider .slideArea').append(slideItem);
		
		// animate
		slideInterval = setInterval(animateLogoSlide,50);
	}
	
	function emailFieldClick(eventObject){
		if($('#footer .socialWrapper #email').attr('value') == 'Enter Your Email') {
			$('#footer .socialWrapper #email').attr('value','');
			$('#footer .socialWrapper #email').css('color','white');
		}
	}
	
	function emailFieldBlur(eventObject){
		if($('#footer .socialWrapper #email').attr('value') == '') {
			$('#footer .socialWrapper #email').attr('value','Enter Your Email');
			$('#footer .socialWrapper #email').css('color','#b7b7b7');
		}
	}
	
	function initEmailField() {
		$('#footer .socialWrapper #email').attr('value','Enter Your Email');
		$('#footer .socialWrapper #email').css('color','#b7b7b7');
		$('#footer .socialWrapper #email').click(emailFieldClick);
		$('#footer .socialWrapper #email').blur(emailFieldBlur);
	}
	
	function getPosition(object,width,height) {
		var position = $(object).offset();
		var dialogBorders = 20;
		var videoWidth = width + dialogBorders;
		var videoHeight = height + dialogBorders;
		var winHeight = $(window).height();
		var containerWidth = $('.centerColorBg').outerWidth();
		
		var scrollTop = null;
		
		if($('body').scrollTop() > 0 && $('html').scrollTop() == 0 )	
			scrollTop = $('body').scrollTop();
		else {
			scrollTop = $('html').scrollTop();
		}
		
		// position relative to link
		//position.top = position.top + $(this).outerHeight() - 10;
		//position.left = position.left - $('.centerColorBg').offset().left + 10;
		
		// center in window
		if(winHeight - videoHeight > 2 ) {
			var centered = (winHeight - videoHeight)/2
			position.top = scrollTop + centered;
		} else {
			position.top = scrollTop + 20;
		}
		var centered = (containerWidth - videoWidth)/2;
		position.left = $('.centerColorBg').offset().left + centered;
		
		return position;
	}
	
	function stayCentered() {
		/*$(document).scroll(function(){
			console.log('left: ' + $(document).scrollLeft());
		});*/
		var containerWidth = $('.centerColorBg').outerWidth();
		var videoWidth = $('#modal_dialog').outerWidth();
		var centered = (containerWidth - videoWidth)/2;
		
		$(window).resize(function() {
			$('#modal_dialog').css('left',$('.centerColorBg').offset().left + centered);
		});
	}
	
	function modal_popup(html,position) {
		// setup the dom
		$('#modal_dialog').remove();
		$('body').append('<div id="modal_dialog"></div>')
		
		$('#modal_overlay').show();
		$('.ie6 #modal_overlay').css('height',$(document).height()+'px');
		
		// load the html
		$('#modal_dialog').load(html,function(){
			// update css position
			$('#modal_dialog').css({
				'top':position.top + 'px',
				'left':position.left + 'px'
			});
			
			//$('html').animate({scrollTop : position.top-20},500);
			
			// add iframe fix for ie6
			//$('#modal_dialog').bgiframe();
			
			stayCentered();
			
			// close button function
			$('#modal_close').click(function(){
				$('#modal_dialog').remove();
				$('#modal_overlay').hide();
				$(window).unbind('resize');
				
				return false;
			});
		});
	}
	
	function initModalOverlay() {
		$('#modal_overlay').click(function(){
			$('#modal_dialog').remove();
			$('#modal_overlay').hide();
			$(window).unbind('resize');
			
			return false;
		});
	}
	
	function initModalAdmobile() {
		$('#playAdmobileVideo, #playAdmobileVideo2, #playAdmobileVideo3').click(function(){
			modal_popup('modal_admobile.php',getPosition(this,960,334));
			return false;
		});
	}
	
	function initModalIntextual() {
		$('#playIntextualVideo, #playIntextualVideo2, #playIntextualVideo3').click(function(){
			modal_popup('modal_intextual.php',getPosition(this,746,344));
			return false;
		});
	}
	
	function initModalContextual() {
		$('#playContextualVideo, #playContextualVideo2, #playContextualVideo3').click(function(){
			modal_popup('modal_contextual.php',getPosition(this,746,344));
			return false;
		});
	}
	
	function initModalOverlayAd() {
		$('#playOverlayAdVideo, #playOverlayAdVideo2, #playOverlayAdVideo3').click(function(){
			modal_popup('modal_overlayad.php',getPosition(this,960,326));
			return false;
		});
	}
	
	function initModalThankYouEmail() {
		$('#playThankYouEmailVideo, #playThankYouEmailVideo2, #playThankYouEmailVideo3').click(function(){
			modal_popup('modal_thankyouemail.php',getPosition(this,400,226));
			return false;
		});
	}
	
	function initModalThankYouOffer() {
		$('#playThankYouOfferVideo, #playThankYouOfferVideo2, #playThankYouOfferVideo3').click(function(){
			modal_popup('modal_thankyouoffer.php',getPosition(this,320,214));
			return false;
		});
	}
	
	function initModalSignup() {
		$('.signupLink').click(function(){
			modal_popup('modal_signup.html',getPosition(this,540,173));
			return false;
		});
	}
	
	function rotateFeatured() {
		
		$('#rotateFeatured .rotate' + rotateTrack).animate({'opacity':0},1000,function(){
				$('#rotateFeatured .rotate' + rotateTrack).css('display','none');
				rotateTrack++;
				if (rotateTrack > rotateItems ) rotateTrack = 1;
				//$('#rotateFeatured ul').children().removeClass('active').eq(rotateTrack-1).addClass('active');
				//$('#rotateFeatured .rotate' + rotateTrack).css('display','block');
				
				$('#rotateFeatured .rotate' + rotateTrack).css('display','block').animate({'opacity':1},1000,function() {
					rotateIntervalID = setTimeout(rotateFeatured,8000);
				});
			});
	}
	
	function initRotateFeatured() {
		rotateIntervalID = setTimeout(rotateFeatured,7000);
	}
	
	$(document).ready(function(){
		// init logo slide if found
		if($('#logoSlider').length>0) {initLogoSlide();}
		
		// init rotate featured if found
		if($('#rotateFeatured').length>0) {$(window).load(initRotateFeatured);}
		
		// footer email field
		initEmailField();
		
		// init modal dialogs
		initModalOverlay();
		initModalAdmobile();
		initModalIntextual();
		initModalContextual();
		initModalOverlayAd();
		initModalThankYouEmail();
		initModalThankYouOffer();
		initModalSignup();
	});
})(jQuery);
