$(document).ready(function(){

//get height of new items
$('.newsContent').each(function(){
	$(this).show();
	var newsHeight = $(this).find('.article-body').outerHeight();
	if ($(this).parents('.popUpItem').find('.popUpLauncher').length === 0){
		$(this).parents('.popUpItem').find('.popUpSingle').data('height', newsHeight);
	} else {
		$(this).parents('.popUpItem').find('.popUpLauncher').data('height', newsHeight);
	}
	$(this).css({
		height: 1,
		width: 1,
		overflow: 'hidden'
	});
});



//popup 

function showPopUp(object, width, height, arrows, news){
	//console.log('showPopUp called');
	var ratio = 1;
	if(height > 500 && news === false){
		ratio = 500 / height;
		height = 500;
		width = width * ratio;
	}
	var popup;
	var popUpTop = ($(window).height() / 2) - (height / 2);
	var popupoverlay = '<div id="popUpOverlay">&nbsp;</div>';
	var title;
	if(object.data('title') === undefined){
		title = "";
	} else {
		title = object.data('title');
	}
	
	if(news){
		popup = '<div id="popUp" class="popUpNews"><div class="padding10"></div></div>';
		popUpTop = 200;
	} else {
		popup = '<div id="popUp" class="popUp"><div class="padding10"></div></div>';
	}
	
	$('body').append(popupoverlay);
	$('body').append(popup);
	
	
	
	$('#popUpOverlay').show();
	$('#popUp').css({
		top: ($(window).height() / 2) - 100,
		left: ($(window).width() / 2) - 100
	}).show().animate({
		height: height + 56,
		width: width + 20,
		top: popUpTop,
		left: ($(window).width() / 2) - (width / 2)
	}, function(){
		if(arrows){
			$('#popUp').append('<a id="popUpPrev" class="popUpArrow prevDisabled" href="#"></a><a id="popUpNext" class="popUpArrow nextDisabled" href="#"></a>');
			popUpArrows(object);
		}
		$('#popUp').append('<a href="#" id="popUpClose"></a><h3 class="gothamBlack popUpHeader">' + title + '</h3>');
		popUpMedia(object, width, height);
	});
}

function resizePopUp(object, width, height, news){
	//console.log('resizePopUp called');
	var ratio = 1;
	if(height > 500 && news === false){
		ratio = 500 / height;
		height = 500;
		width = width * ratio;
	}
	var popUpTop = ($(window).height() / 2) - (height / 2);
	
	if(news){
		popUpTop = 200;
	}
	
	
	$('#youtube-player').remove();
	$('#image-holder').remove();
	$('#news-holder').remove();
	$('.popUpHeader').text(object.data('title'));
	$('#popUp').animate({
		height: height + 56,
		width: width + 20,
		top: popUpTop,
		left: ($(window).width() / 2) - (width / 2)
	}, function(){
		popUpArrows(object);
		popUpMedia(object, width, height);
	});
}

function popUpMedia(object, width, height){
	//console.log('popUpMedia called');
	
	switch(object.data('media')){
		case 'video':
			$('.popUp .padding10').append('<div id="youtube-player"><object width="500" height="306"><param name="movie" value="http://www.youtube.com/v/' + object.data('id') + '?fs=1&amp;hl=en_US&amp;rel=0&amp;autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + object.data('id') + '?fs=1&amp;hl=en_US&amp;rel=0&amp;autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="500" height="344"></embed></object></div>');
			break;
		case 'image':
			$('.popUp .padding10').append('<div id="image-holder"><img src="' + object.attr('href') + '" alt="' + object.data('title') + '" width="' + width + '" height="' + height + '">');
			break;
		case 'news':
			$('html,body').animate({scrollTop:0}, 600);
			$('.popUpNews .padding10').append('<div id="news-holder">' + object.parents('.popUpItem').find('.newsContent').html() + '</div>');
			break;
	}
}

function popUpArrows(object){
	//console.log('popUpArrows Called');
	var $container = object.parents('.popUpContainer');
	var itemIndex = $container.find('.popUpItem').index(object.parents('.popUpItem')) + 1;
	var totalItems = $container.find('.popUpItem').length;
	
	$container.find('.popUpItem').eq(itemIndex - 1).addClass('now-playing');
	
	//console.log(itemIndex + ' of ' + totalItems);
	if (itemIndex === 1){
		$('#popUpPrev').removeClass('prevEnabled').addClass('prevDisabled');
		$('#popUpNext').removeClass('nextDisabled').addClass('nextEnabled');
	} else if (itemIndex === totalItems) {
		$('#popUpNext').removeClass('nextEnabled').addClass('nextDisabled');
		$('#popUpPrev').removeClass('prevDisabled').addClass('prevEnabled');
	} else {
		$('#popUpPrev').removeClass('prevDisabled').addClass('prevEnabled');
		$('#popUpNext').removeClass('nextDisabled').addClass('nextEnabled');
	}
}

	$('.popUpSingle').live('click', function(){
		//console.log('popUpSingle');
		var isNews = false;
		var height = $(this).data('height')
	if($(this).data('media') === 'news'){
			isNews = true
			if($(this).data('padding') === undefined){
				height = height + 370;
			} else {
				height = height + $(this).data('padding');
				//console.log('adding padding');
			}
		}		showPopUp($(this), $(this).data('width'), height, false, isNews);
	 
	return false;
	});
	
	$('.popUpLauncher').live('click', function(){
		//console.log('popUpLauncher');
		var isNews = false;
		var height = $(this).data('height')
		if($(this).data('media') === 'news'){
			isNews = true
			if($(this).data('padding') === undefined){
				height = height + 370;
			} else {
				height = height + $(this).data('padding');
			}
		}
		showPopUp($(this), $(this).data('width'), height, true, isNews);
		return false;
	});
	
	$('#popUpClose').live('click', function(){
		$('#popUpOverlay').remove();
		$('#popUp').remove();
		return false;
	});
	
	
	$('.nextEnabled').live ('click', function(){
		//console.log('---- next button ----');
	
		var nextObject = $('.now-playing').next().find('a').eq(0);
		//console.log(nextObject);
		$('.now-playing').removeClass('now-playing');
		var isNews = false;
		nextObject.data('media') === 'news' ? isNews = true : isNews = false;
		resizePopUp(nextObject, nextObject.data('width'), nextObject.data('height'), isNews);
		return false;
	});
	
	$('.prevEnabled').live ('click', function(){
		//console.log('---- prev button ----');
		var prevObject = $('.now-playing').prev().find('a').eq(0);
		var isNews = false;
		prevObject.data('media') === 'news' ? isNews = true : isNews = false;
		resizePopUp(prevObject, prevObject.data('width'), prevObject.data('height'), isNews);
		return false;
	});
	
	$('.prevDisabled').live('click', function(){return false;});
	
	$('.nextDisabled').live('click', function(){return false;});
	
	$('#popUpOverlay').live('click', function(){
		$('.now-playing').removeClass('now-playing');
		$('#faqsContainer').remove();
		$('#paymentFaqsContainer').hide();
		$('#popUpOverlay').remove();
		$('#popUp').remove();
		$('#addToBasketPopUp').remove();
		$('#contactForm').hide();
		$('#sizePopUp .padding15').hide();
		$('#sizePopUp').css({
			'display': 'none',
			'width': 200,
			'height': 200,
			'margin-left': -100,
			'margin-top': -100,
			'top': '50%'
		});
		return false;
	});
	
});
