var current = {	
	path : {
		js  : '',
		url : '',
		arr : ''
	},
	object : {},
	index  : 0
};

var assets = {
 
	broadcast : {
		bipolar		: [{ title: 'Bipolar Options',	client : 'BMS' }],
		dermatology	: [{ title: 'Dermatology',	client : 'Humira' }],
		levemir		: [{ title: 'Levemir',		client : 'Levemir' }],
		grim			: [{ title: 'Grim News',	client : 'Time Warner Cable' }],
		spider		: [{ title: 'Spider',		client : 'Time Warner Cable' }],
		bogey			: [{ title: 'Bogey',			client : 'Time Warner Cable' }],
		phone			: [{ title: 'Phone - Gen',	client : 'Time Warner Cable' }],
		small			: [{ title: 'Small World',	client : 'Time Warner Cable' }],
		stream		: [{ title: 'Stream of Consciousness',	client : 'Time Warner Cable',	index : {	start: 1,	end: 5	}	}],
		wall			: [{ title: 'Spider',		client : 'Time Warner Cable' }]
	},
 
	interactive : {
		welcome		: [{ title: 'Welcome',		client : 'AARP' }],
		hco			: [{ title: 'Health Care Options',	client : 'AARP' }],
		yaz			: [{ title: 'Yaz',			client : 'Bayer' }],
		quitwin		: [{ title: 'Quit2Win',		client : 'GSK' }],
		smoking		: [{ title: 'Smoking',		client : 'GSK' }],
		destroy		: [{ title: 'Destroy',		client : 'Samsung' }],
		dreamhd		: [{ title: 'Dream in HD',	client : 'Time Warner Cable' }],
		alien			: [{ title: 'Alienware',	client : 'Time Warner Cable' }],
		clause		: [{ title: 'Fred Clause',	client : 'Time Warner Cable' }]
	},
	
	print : {
		angeliq		: [{ title: 'Angeliq',		client : 'Bayer' }],
		fitness		: [{ title: 'Fitness',		client : 'AARP' }],
		climara		: [{ title: 'Climara',		client : 'Bayer' }],
		pilot			: [{ title: 'MRM Pilot',	client : 'AARP' }],
		travel		: [{ title: 'Travel',		client : 'AARP' }],
		yoga			: [{ title: 'Yoga',			client : 'AARP' }],
		abilify		: [{ title: 'Abilify',		client : 'BMS' }],
		dm				: [{ title: 'DM',				client : 'AARP' }],
		mirena		: [{ title: 'Mirena',		client : 'Bayer' }]
	}
	
};

var isArray = function() {
	if (typeof arguments[0] == 'object') {
		var criterion = arguments[0].constructor.toString().match(/array/i); 
		return (criterion != null);
	}
	return false;
};

function createArray(ppt) {
	var tempArray = new Array();
	if (ppt.index.start > ppt.index.end) {
		for ( i = ppt.index.start;	i >= ppt.index.end;	i-- ) {
			tempArray.push({	title : ppt.title + i,	client : ppt.client,	page : i });
		}
	} else {
		for ( i = ppt.index.start;	i <= ppt.index.end;	i++ ) {
			tempArray.push({	title : ppt.title,	client : ppt.client,	page : i });
		}
	}
	return tempArray;
}
	
function objectCopy(obj) {
	this.title  = obj.title;
	this.client = obj.client;
	this.index  = obj.index;
}

var isNewSection = true;

var display = function(item, id) {
	var path = (item.page !== undefined) ? current.path.url +"/"+ item.page +".flv" : current.path.url+'/1.flv';
	if (isNewSection) {
		$("div#"+id+' div.flash').empty().prepend(video.embed(path,video.size.medium)).delay(50, function(){
			video.player = $(this).find('object').get(0);
		});
		isNewSection = false;
	} else {
		try {
			video.player.playVideo(path);
		} catch(e) {
			$("div#"+id+' div.flash').empty().prepend(video.embed(path,video.size.medium)).delay(50, function(){
				video.player = $(this).find('object').get(0);
			});
		}
	}
};

var pages = {
	show : function(id) {
		var pageHTML = '';
		for (var i=1; i <= current.object.length; i++) {
			pageHTML = (i == 1) ? pageHTML +'<a class="sel" href="#'+ current.path.js +'.'+ i +'">'+ i +'</a>' : pageHTML +'<a href="#'+ current.path.js +'.'+ i +'">'+ i +'</a>';
			pageHTML = (i != current.object.length) ? pageHTML + ' | ' : pageHTML;
		};
		$('#'+id+' div.caption div.page').empty().prepend(pageHTML).show();
	},
	hide : function(id) {
		$('#'+id+' div.caption div.page').hide();
	}
}

$(document).ready(function() {
	var temp;
	$('div.medium').each(function(){
		var jq = $(this);
		var id = jq.attr('id');
		
		$("#"+id+" div.small").unbind("click").bind("click", function(e){
			e.preventDefault();
			isNewSection = true;
			var opened = $('div.big:visible').closest('div.medium');
			$("#contentNav ul li a").removeClass("sel").filter("a[href="+$(this).find('a').attr('href')+"]").addClass("sel");
			if (opened.length != 0) {
				$("div.header h3:last").text(id).removeClass("sIFR-replaced");
				opened.find("div.flash").empty().end()
			       .find("div.big").animate({"delay": 1}, { queue:true, duration:0, complete: function(){
			             $(this).slideUp(500);
			          }})
			       .end()
			       .find("div.small")
			          .animate({"delay": 1}, { queue:true, duration:0, complete: function(){
			             $(this).slideDown(500);
			          }});
			} else {
				$("div.header").append("<h3>"+id+"</h3>");
				
			}
			jq.find("div.small")
				.animate({"delay": 1}, { queue:true, duration:0, complete: function(){
					$(this).slideUp(500);
				}})
				.end()
				.find("div.big").animate({"delay": 1}, { queue:true, duration:0, complete: function(){
					$(this).slideDown(500, function(){
						$(this).find("ul.projects a:first").click();
					})
				}});
			replaceH3();
			return false;
		}).hover(
			function() {
				temp = $("#contentNav ul li a").filter("a[href="+$(this).find('a').attr('href')+"]").addClass("hover");
			}, function() {
				temp.removeClass("hover");
			}
		);
		
		$("#"+id+" ul.projects a").unbind("click").bind("click", function(e){
			e.preventDefault();
			$("#"+id+" ul.projects a.sel").removeClass("sel");
			$(this).addClass("sel");
			current.path.js  = $(this).attr("href").replace('#','');
			current.path.arr = current.path.js.split(".");
			current.path.url = '/include/portfolio/' + current.path.arr.join("/");
			current.object   = eval('assets.'+current.path.js);
			current.index    = 0;
			
			if ( isArray(current.object) && ( current.object[0].index !== undefined )) {
				ppt = new objectCopy(current.object[0]);
				current.object = createArray(ppt);
			}
			
			if (current.object.length > 1) { pages.show(id) } else { pages.hide(id) }
			$('div#'+id).find("div.caption")
				.find("h4")
					.html(current.object[0].client)
				.end()
				.find("h5")
					.html("&ldquo;"+current.object[0].title+"&rdquo;");
			display(current.object[0], id);
			return false;
		});
		
		$("div.page a").die("click").live("click",function(e){
			e.preventDefault();
			$(this).addClass("sel").siblings("a.sel").removeClass("sel");
			display(current.object[$(this).text()-1], id);
			return false;
		});
		
		
	});
	
	$("div#contentNav ul li a").click(function() {
		$("div.small a").filter("a[href="+$(this).attr('href')+"]").trigger("click");
		return false;
	}).hover(
		function() {
			temp = $("div.small a").filter("a[href="+$(this).attr('href')+"]").addClass("hover");
		}, function() {
			temp.removeClass("hover");
		}
	);

});
