
var portfolio = function() {

	// private variables
	var campaignId;
	var itemCount = 0;
	var idx = 0;
	
	
	return {
	
		init : function (){
			$("div.campaign").hover(
				function(){
					campaignId = this.id;
					$(this).addClass("hover");
				},
				function(){
					$(this).removeClass("hover");
				}
			);
			
			$("div.campaign").click(function(){
				portfolio.campaign.init();
			});
		},
		
		campaign : {
				
			init : function () {
			
				// get the hidden HTML that holds all the campaing detail content
				var detailHtml = $("div#"+campaignId+" div.detail").html();

				// shove the HTML into the modal and display it
				$("div#campaign-pop-content").html(detailHtml);
				
				$("div#campaign-pop-content").show();

				$('div#campaign-pop').modal({
					overlayClose:true
				});
				
				// set-up the prev/next thumnbnail controls
				portfolio.campaign.initControls();

				// show the first item in campaign
				idx=0;
				portfolio.campaign.showItem();
				
			},
			
			showItem : function () {
			
				$("div#campaign-pop-content div.item").hide();
				var itemContainer = "div#campaign-pop-content div#item-"+idx;
				$(itemContainer).show();
				
				$("div#flashcontent").remove();
				$(itemContainer+" img.large-image").remove();
				
				if ($("div#item-"+idx+" span.flash").length > 0){
				
					var flashId = $("div#item-"+idx+" span")[0].id;
					
					$("div#item-"+idx).append("<div id='flashcontent'></div>");

					var swfInfo = flashId.split("|");
					
					var so = new SWFObject("_res/flash/"+swfInfo[0]+"", "swf", ""+swfInfo[1]+"", ""+swfInfo[2]+"", "8", "#101010");
					so.useExpressInstall('_res/flash/expressinstall.swf');
					so.write('flashcontent')
				}
				else {
				
					//$(itemContainer).append("<span class='loading' />");
					
					//debugger;
					
					if ($(itemContainer+" a.large-image:eq(0)").length > 0){
					
						var imgSrc = $(itemContainer+" a.large-image:eq(0)").attr("href");
						var img = new Image();

						$(img).load(function () {

							$(itemContainer).append(this);

							//$(itemContainer+" span.loading").remove();

						}).attr('src', imgSrc).attr('class', 'large-image');
					
					}
					
					
				}
							
				portfolio.campaign.activateThumb();
				portfolio.campaign.setPrevNextControls();

			},
			
			initControls : function () {
			
				// build the set of thumbnails
				$('div#campaign-pop-content div.item').each(function(i){
					
					this.id = 'item-'+i;
					$('div#thumbset').append("<span id=thumb-"+i+" />");
					itemCount++;
				})

				$('div#thumbset span').click(function(){
					idx = portfolio.getIndex(this.id);
					portfolio.campaign.showItem();
				});
				
				
				
			},
			
			activateThumb : function () {
				
				$("div#thumbset span").removeClass("active");
				$("span#thumb-"+idx).addClass("active");
			
			},
			
			setPrevNextControls : function () {
								
				$("span#left-control").removeClass().unbind('click');
				$("span#right-control").removeClass().unbind('click');


				if (idx == 0)
					$("span#left-control").addClass("leftDeactivated");
				else
					$("span#left-control").addClass("leftActivated");
				
				if ((idx+1) == itemCount)
					$("span#right-control").addClass("rightDeactivated");
				else 
					$("span#right-control").addClass("rightActivated");
					
				// set up the previous and next arrow controls
				$("span.leftActivated").click(function(){
					idx--;			
					portfolio.campaign.showItem();
				});

				$("span.rightActivated").click(function(){
					idx++;	
					portfolio.campaign.showItem();
				});		
					
			}
		
		},
		
		getIndex : function (item) {
		
			idx = parseFloat(item.split("thumb-")[1]);
			
			return idx;

		}
	};
}();


var global = function() {

	// private variables
	
	return {
	
		init : function (){
			$("div#header ul li").hover(
				function(){
					$(this).addClass("hover");

				},
				function(){
					$(this).removeClass("hover");
				}
			)
		
		}
	};
}();

$(document).ready(function(){
	global.init();
	portfolio.init();
});








