var loadInIframe = function(hash){
	var $trigger = $(hash.t);
	var $modal = $(hash.w);
	var myUrl = $trigger.attr('href');
	// Create iframe here to keep popup content out of browser history
	$(document.createElement('iframe'))
		.attr('allowtransparency','yes')
		.attr('height','100%')
		.attr('width','100%')
		.attr('scrolling','no')
		.attr('frameborder','0')
		.attr('marginwidth','0')
		.attr('marginheight','0')
		.attr('id','popup-content')
		.appendTo($modal); 
	var $modalContent = $("iframe", $modal);
	$modalContent.html('').attr('src', myUrl);
	//$modal.jqmShow();
	hash.w.show();
}

var loadInIframeFeat = function(myUrl){
	$(document.createElement('iframe'))
		.attr('allowtransparency','yes')
		.attr('height','100%')
		.attr('width','100%')
		.attr('scrolling','no')
		.attr('frameborder','0')
		.attr('marginwidth','0')
		.attr('marginheight','0')
		.attr('id','popup-content')
		.attr('src', myUrl)
		.appendTo('#modalWindow');
}

var iframeReset = function(hash){
	var $modal = $(hash.w);
	$('#popup-content').remove();
	$modal.css('display','none');
//	$('#modalWindow').find('iframe').attr('src', 'blank.html');
}

$(document).ready(function(){
	$("#featured-slides").tabs({fx:{opacity: "toggle"}});


	$('#modalWindow').jqm({
		modal: true,
		trigger: 'a.workpopup',
		target: '#jqmContent',
		onShow:  loadInIframe,
		onHide: iframeReset
	});
	
		
	$('.external').attr('target','_blank');
	
	$('form#contactform').validate();
	
	// Because IE7 was being a bitch.
	$('ul#topnav li a').html(null);

	$('#workgrid img').hover(function(){
		imgurl = $(this).attr('src');
		var hoverimgurl = imgurl.replace('.jpg','-hover.jpg');
		$(this).attr('src', hoverimgurl);
	},
	function(){
		$(this).attr('src', imgurl);
	});


	$('#work-page-grid .item').hover(function(){
		if ($("#work-page-popup-outer").length == 0){ // Check to make sure there is not a work popup open
			$(this).children('.item-image').stop(true, true).fadeOut(200);
			$(this).find('.title').stop(true, true).animate({
				top:	'10px'
			}, 200);
			$(this).find('.view-now').stop(true, true).animate({
				bottom:	'13px'
			}, 200);
		}
	}, function(){
		$(this).children('.item-image').stop(true, true).fadeIn(200);
		$(this).find('.title').stop(true, true).animate({
			top:	'-50px'
		}, 200);
		$(this).find('.view-now').stop(true, true).animate({
			bottom:	'-50px'
		}, 200);
	});
	
	$('#work-page-grid a').click(function(){	
		if ($("#work-page-popup-outer").length == 0){ // Check to make sure there is not a work popup open
			// Open popup
			var popup_url = $(this).attr('href');
			var popup_html = '<div id="work-page-popup-outer">\b\
								<div id="work-page-popup-inner">\b\
								</div>\b\
								<div id="work-page-popup-close"><img src="http://viewstream.com/images/popup-close.gif" height="29" width="29" alt="Close" /></div>\b\
							</div>';
			$('body').append(popup_html);
			$('#header').after('<div id="work-page-overlay"></div>');
			$('#work-page-popup-inner').load(popup_url, function(){
				// Slideshow
				var slide_count = $('#work-page-popup-inner .slides li').length;
				$('.full-count').html(slide_count);
				
				$('.arrows .next').click(function(){
					var current_slide = parseInt($('.counter .current').html());
					if(current_slide < slide_count){
						$('.slides .current').fadeOut(150, function(){
							$(this).next().fadeIn(150, function(){
								$('.slides .current').removeClass('current');
								$(this).addClass('current');
								var new_slide_num = current_slide + 1;
								$('.counter .current').html(new_slide_num)
								if(new_slide_num == slide_count){
									$('.arrows .next').attr('src', '../images/slide-right-inactive.png');
								}
								if(new_slide_num == 2){
									$('.arrows .previous').attr('src', '../images/slide-left.png');
								}
							});
						});
					}
					work_id = popup_url.split('/')[1].split('-');
					_gaq.push(['_trackEvent', 'Work', 'Next', work_id[0].substr(0,1).toUpperCase()+work_id[0].substr(1) +' '+ work_id[1]]);
				});
				
				$('.arrows .previous').click(function(){
					var current_slide = parseInt($('.counter .current').html());
					if(current_slide != 1){
						$('.slides .current').fadeOut(150, function(){
							$(this).prev().fadeIn(150, function(){
								$('.slides .current').removeClass('current');
								$(this).addClass('current');
								var new_slide_num = current_slide - 1;
								$('.counter .current').html(new_slide_num)
								if(new_slide_num == 1){
									$('.arrows .previous').attr('src', '../images/slide-left-inactive.png');
								}
								if(new_slide_num == slide_count - 1){
									$('.arrows .next').attr('src', '../images/slide-right.png');
								}
							});
						});
					}
					work_id = popup_url.split('/')[1].split('-');
					_gaq.push(['_trackEvent', 'Work', 'Previous', work_id[0].substr(0,1).toUpperCase()+work_id[0].substr(1) +' '+ work_id[1]]);
				});
	
				$('#work-page-popup-close').click(function(){
					$('#work-page-popup-outer').fadeOut(300, function(){
						$(this).remove();
					});
					$('#work-page-overlay').animate({
						opacity: 0
					}, 300, function(){
						$(this).remove();
					});
					work_id = popup_url.split('/')[1].split('-');
					_gaq.push(['_trackEvent', 'Work', 'Close', work_id[0].substr(0,1).toUpperCase()+work_id[0].substr(1) +' '+ work_id[1]]);
				});
				
				$('#work-page-popup-outer').fadeIn(300);
				$('#work-page-overlay').animate({
					opacity: 0.5
				}, 300);
			})
		}
		
		return false;
		
	});
	
	
		///services interactions	
	$(".services-item img").hover(function () {
		if(!$($(this).parent('a')).hasClass('active')){
			this.src = this.src.replace("-stat","-roll");
		}
	},function(){
					if(!$($(this).parent('a')).hasClass('active')){
		 				 this.src = this.src.replace("-roll","-stat");
					}
			 	});
			
	var images = [
					'<?php baseUrl(); ?>/images/services-conversion-roll.png',
					'<?php baseUrl(); ?>/images/services-content-roll.png',
					'<?php baseUrl(); ?>/images/services-strategy-roll.png'
				];
	$(images).each(function() {
		var image = $('<img />').attr('src', this);
	});	
	
	$('#work-page-grid a').click(function (){
	work_id = $(this).attr('href').split('/')[1].split('-');
	_gaq.push(['_trackEvent', 'Work', 'Click', work_id[0].substr(0,1).toUpperCase()+work_id[0].substr(1) +' '+ work_id[1]]);
	});
	
	$('.carousel_cta').click(function(){
		carousel_cta = $(this).attr('id').split('_');
		_gaq.push(['_trackEvent', 'Home', 'Button Click', carousel_cta[0].substr(0,1).toUpperCase()+carousel_cta[0].substr(1)]);
		window.location = $('a', this).attr('title');
	});
});
