var newImgTransitioning = 0;

window.flashSizes = new Array();

(function($) {
	var cache = [];
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)

function launchFlash(galleryFlashLaunchID, SWFHeight, SWFWidth, SWFLocation) {

	$(galleryFlashLaunchID).click(function(){
		$('#cboxBottomCenter').html('');
		$.fn.colorbox({href: 'flash.html', innerWidth: ''+SWFWidth+'', innerHeight: ''+SWFHeight+'', onComplete:function(){ 
			flashString = '<object width="'+SWFWidth+'" height="'+SWFHeight+'" id="megazine"><param name="allowfullscreen" value="true" /><param name="wmode" value="opaque" /><param name="allowscriptaccess" value="always" /><param name="movie" value="flash/preloader/preloader.swf"" /><param name="FlashVars" value="swfLocation='+SWFLocation+'" /><embed src="flash/preloader/preloader.swf" type="application/x-shockwave-flash" FlashVars="swfLocation='+SWFLocation+'" wmode="opaque" allowscriptaccess="always" width="'+SWFWidth+'" height="'+SWFHeight+'"></embed></object>';
			$('#cboxLoadedContent div').html(flashString);
			}
		});
	});
}

function launchImage(galleryFlashLaunchID, ImgName) {

	$(galleryFlashLaunchID).click(function(event){
		$('#cboxBottomCenter').html('');
		$.fn.colorbox({href: 'images/work/bigimages/'+ImgName+'', scrolling: false});

	});
}

function launchPrint(galleryFlashLaunchID, XMLLocation) {

	$(galleryFlashLaunchID).click(function(){
		$('#cboxBottomCenter').html('');
		$.fn.colorbox({href: 'flash.html', innerWidth: '780', innerHeight: '550', onComplete:function(){ 
			flashString = '<div id="megazine">&nbsp;</div>';
			$('#cboxLoadedContent div').html(flashString);
			swfobject.embedSWF("flash/megazine/megazine.swf", "megazine", "100%", 550, "9.0.115", "flash/megazine/expressInstall.swf", { xmlFile : ""+XMLLocation+"", minScale : 1.0, maxScale : 1.0, top: "0" }, { bgcolor : "#fffff", allowFullscreen : "true" }, {id : "megazine"});
			}
		});
	});
}

function launchEmail(galleryFlashLaunchID, newImgFileName) {

	$(galleryFlashLaunchID).click(function(){

		$.fn.colorbox({href: 'email.html', scrolling: false, innerWidth: 720, innerHeight: 650, onComplete:function(){ 
			newEmailImgPath = "images/work/emails/"+newImgFileName;
			$("#galleryColorBoxImg").attr("src",newEmailImgPath);
			$('#cboxBottomCenter').html('<div style="position: absolute; top: 678px; left: 25px;"><a href="#" id="galleryColorBoxUp" style="outline: none;"><img id="galleryColorBoxUpImg" border="0" src="images/up_colorbox.png" style="outline: none;"></a>&nbsp;<a href="#" id="galleryColorBoxDown" style="outline: none;"><img id="galleryColorBoxDownImg" border="0" src="images/down_colorbox.png" style="outline: none;"></a></div>');

			$('#galleryColorBoxUp').hover( function () { 
				$('#galleryColorBoxUpImg').attr('src', 'images/up_active_colorbox.png'); 
			}, 
									
				function () {
					$('#galleryColorBoxUpImg').attr('src', 'images/up_colorbox.png'); 
				}
			);

			$('#galleryColorBoxDown').hover( function () { 
				$('#galleryColorBoxDownImg').attr('src', 'images/down_active_colorbox.png'); 
			}, 

				function () {
					$('#galleryColorBoxDownImg').attr('src', 'images/down_colorbox.png'); 
				}
									
			);
							
			$('#galleryColorBoxUp').click(function(){
				$('#cboxLoadedContent').scrollTo( {top:'-=500', left:'0' }, 500 );
			        return false;
			    });

			$('#galleryColorBoxDown').click(function(){
				$('#cboxLoadedContent').scrollTo( {top:'+=500', left:'0' }, 500 );
			        return false;
			    });
						
			}

		});

	});

}

function createGalleryInstance(galleryImgID, galleryControlsID, galleryPrevID, galleryNextID, galleryType, galleryFlashLaunchID, SWFLocation, SWFHeight, SWFWidth) {

	$(galleryControlsID+" li a").wrapInner("<span></span >");
	$(galleryControlsID+" li a span").css({"opacity" : 0});

	if (galleryType == 'multiLauncher'){

		if ( !($(galleryControlsID+" li.current").find('a').hasClass('launchmail')) && !($(galleryControlsID+" li.current").find('a').hasClass('launchflash')) && !($(galleryControlsID+" li.current").find('a').hasClass('launchimage')) && !($(galleryControlsID+" li.current").find('a').hasClass('launchprint')) ) {
			$(galleryFlashLaunchID).addClass('galleryHide');
			$(galleryPrevID).removeClass('galleryMultiPrev');
			$(galleryPrevID).addClass('galleryPrev');
			$(galleryNextID).removeClass('galleryMultiNext');
			$(galleryNextID).addClass('galleryNext');
		} else {

		
			if ($(galleryControlsID+" li.current").find('a').hasClass('launchmail')) {
				$(galleryFlashLaunchID).unbind( "click" );
				newImgFileName = $(galleryControlsID+" li.current").find('a').attr("href").replace(/.+[\/]([^\/]+)$/,'$1');				
				launchEmail(galleryFlashLaunchID, newImgFileName);
			}

			if ($(galleryControlsID+" li.current").find('a').hasClass('launchflash')) {
				$(galleryFlashLaunchID).unbind( "click" );
				newImgFileName = $(galleryControlsID+" li.current").find('a').attr("href").replace(/.+[\/]([^\/]+)$/,'$1');
				newImgFileName = newImgFileName.replace(/(.*)\.[^.]+$/,'$1');
				SWFHeight = window.flashSizes[newImgFileName+'.swf']['h'];
				SWFWidth = window.flashSizes[newImgFileName+'.swf']['w'];
				newImgFileName = './images/work/flash/'+newImgFileName+'.swf';		
				launchFlash(galleryFlashLaunchID, SWFHeight, SWFWidth, newImgFileName);
			}
			
			if ($(galleryControlsID+" li.current").find('a').hasClass('launchimage')) {
				$(galleryFlashLaunchID).unbind( "click" );
				newImgFileName = $(galleryControlsID+" li.current").find('a').attr("href").replace(/.+[\/]([^\/]+)$/,'$1');				
				launchImage(galleryFlashLaunchID, newImgFileName);
			}

			if ($(galleryControlsID+" li.current").find('a').hasClass('launchprint')) {
				$(galleryFlashLaunchID).unbind( "click" );
				newImgFileName = $(galleryControlsID+" li.current").find('a').attr("href").replace(/.+[\/]([^\/]+)$/,'$1');
				newImgFileName = newImgFileName.replace(/(.*)\.[^.]+$/,'$1')+'.xml';				
				launchPrint(galleryFlashLaunchID, newImgFileName);
			}		

		}
		
	}

	$(galleryControlsID+" li a").hover(function(){
		if($(this).hasClass("active") == false) {
			$(this).children("span").animate({"opacity" : 1}, 150);
		}
	}, function(){
			$(this).children("span").animate({"opacity" : 0}, 75);
	});

	$(galleryPrevID).hover(function(){
		$(galleryPrevID+" img").fadeIn(200); 
	},
	function(){
		$(galleryPrevID+" img").fadeOut(150);
	});
	
	$(galleryNextID).hover(function(){
		$(galleryNextID+" img").fadeIn(200); 
	},
	function(){
		$(galleryNextID+" img").fadeOut(150);
	});	

        if (galleryType == 'flashLauncher') {
                $(galleryFlashLaunchID).hover(function(){
                        $(galleryFlashLaunchID+" img").fadeIn(200);
                },
                function(){
                        $(galleryFlashLaunchID+" img").fadeOut(150);
                });

        }

        if (galleryType == 'printLauncher') {
                $(galleryFlashLaunchID).hover(function(){
                        $(galleryFlashLaunchID+" img").fadeIn(200);
                },
                function(){
                        $(galleryFlashLaunchID+" img").fadeOut(150);
                });

        }		
		
        if (galleryType == 'multiLauncher') {
                $(galleryFlashLaunchID).hover(function(){
                        $(galleryFlashLaunchID+" img").fadeIn(200);
                },
                function(){
                        $(galleryFlashLaunchID+" img").fadeOut(150);
                });

        }
	
	$(galleryNextID).click(function(){
		if($(galleryControlsID+" li.current").is(':last-child')) {
			changeGalleryItem($(galleryControlsID+" li.current").siblings(':first-child').find('a'));
		} else {
			changeGalleryItem($(galleryControlsID+" li.current").next().find('a'));
		}
	});
	
	$(galleryPrevID).click(function(){
		if($(galleryControlsID+" li.current").is(':first-child')) {
			changeGalleryItem($(galleryControlsID+" li.current").siblings(':last-child').find('a'));
		} else {
			changeGalleryItem($(galleryControlsID+" li.current").prev().find('a'));
		}
	});

	if (galleryType == 'printLauncher') {
		
		launchPrint(galleryFlashLaunchID, SWFLocation);
		
	}
	
	if (galleryType == 'flashLauncher') {
		
		launchFlash(galleryFlashLaunchID, SWFHeight, SWFWidth, SWFLocation);
		
	}	

	$(galleryControlsID+" a").click(function(){
		changeGalleryItem($(this));
		return false;
	});

	function changeGalleryItem(targetItem) {
	
		if(newImgTransitioning == 0) {
		
		newImgTransitioning = 1;
			
		$(galleryControlsID+" a").removeClass('active');
		targetItem.addClass('active');
		$(galleryControlsID+" li").removeClass('current');
		targetItem.parent().addClass('current');
		
		var newImgPath = targetItem.attr("href");
		var newImgAlt = targetItem.attr("title");
		var newImgFileName = targetItem.attr("href").replace(/.+[\/]([^\/]+)$/,'$1');

	    if (galleryType == 'multiLauncher') {

			if ( !($(galleryControlsID+" li.current").find('a').hasClass('launchmail')) && !($(galleryControlsID+" li.current").find('a').hasClass('launchflash')) && !($(galleryControlsID+" li.current").find('a').hasClass('launchimage')) && !($(galleryControlsID+" li.current").find('a').hasClass('launchprint')) ) {

				$(galleryFlashLaunchID).addClass('galleryHide');
				$(galleryPrevID).removeClass('galleryMultiPrev');
				$(galleryPrevID).addClass('galleryPrev');
				$(galleryNextID).removeClass('galleryMultiNext');
				$(galleryNextID).addClass('galleryNext');

		} else {			
			
				$(galleryFlashLaunchID).removeClass('galleryHide');
				$(galleryPrevID).removeClass('galleryPrev');
				$(galleryPrevID).addClass('galleryMultiPrev');
				$(galleryNextID).removeClass('galleryNext');
				$(galleryNextID).addClass('galleryMultiNext');

			}
			
			if ($(galleryControlsID+" li.current").find('a').hasClass('launchmail')) {
				$(galleryFlashLaunchID).unbind( "click" );
				newImgFileName = $(galleryControlsID+" li.current").find('a').attr("href").replace(/.+[\/]([^\/]+)$/,'$1');				
				launchEmail(galleryFlashLaunchID, newImgFileName);
			}

			if ($(galleryControlsID+" li.current").find('a').hasClass('launchflash')) {
				$(galleryFlashLaunchID).unbind( "click" );
				newImgFileName = $(galleryControlsID+" li.current").find('a').attr("href").replace(/.+[\/]([^\/]+)$/,'$1');
				newImgFileName = newImgFileName.replace(/(.*)\.[^.]+$/,'$1');
				SWFHeight = window.flashSizes[newImgFileName+'.swf']['h'];
				SWFWidth = window.flashSizes[newImgFileName+'.swf']['w'];
				newImgFileName = './images/work/flash/'+newImgFileName+'.swf';		
				launchFlash(galleryFlashLaunchID, SWFHeight, SWFWidth, newImgFileName);
			}
			
			if ($(galleryControlsID+" li.current").find('a').hasClass('launchimage')) {
				$(galleryFlashLaunchID).unbind( "click" );
				newImgFileName = $(galleryControlsID+" li.current").find('a').attr("href").replace(/.+[\/]([^\/]+)$/,'$1');				
				launchImage(galleryFlashLaunchID, newImgFileName);
			}

			if ($(galleryControlsID+" li.current").find('a').hasClass('launchprint')) {
				$(galleryFlashLaunchID).unbind( "click" );
				newImgFileName = $(galleryControlsID+" li.current").find('a').attr("href").replace(/.+[\/]([^\/]+)$/,'$1');
				newImgFileName = newImgFileName.replace(/(.*)\.[^.]+$/,'$1')+'.xml';				
				launchPrint(galleryFlashLaunchID, newImgFileName);
			}			
			
		}
		
		$(galleryImgID).parent().css('background-image', 'url(' + newImgPath + ')');
		$(galleryImgID).parent().css('background-repeat', 'no-repeat');
	
		$(galleryImgID).fadeOut(350, function() {
			$(galleryImgID).attr({ src: newImgPath, alt: newImgAlt });
			$(galleryImgID).show();
			newImgTransitioning = 0;
		});
			
		$(galleryControlsID+" a").removeClass('active');
		targetItem.addClass('active');
		$(galleryControlsID+" li").removeClass('current');
		targetItem.parent().addClass('current');
		targetItem.children("span").animate({"opacity" : 0}, 75);
			
		}

	}

}

$(document).ready(function(){

	jQuery.preLoadImages("./images/work/vibe_top.jpg", "./images/work/vibe_2nd.jpg", "./images/work/vibe_3rd.jpg", "./images/work/vibe_4th.jpg", "./images/work/vibe_5th.jpg","./images/work/prada_top.jpg", "./images/work/prada_2nd.jpg", "./images/work/prada_3rd.jpg", "./images/work/prada_4th.jpg", "./images/work/prada_5th.jpg", "./images/work/fold_top.jpg", "./images/work/fold_2nd.jpg", "./images/work/fold_3rd.jpg", "./images/work/fold_4th.jpg", "./images/work/fold_5th.jpg", "./images/work/fold_6th.jpg", "./images/work/fold_7th.jpg", "./images/work/fold_8th.jpg", "./images/work/psp_top.jpg", "./images/work/psp_2nd.jpg", "./images/work/psp_3rd.jpg", "./images/work/psp_4th.jpg", "./images/work/psp_5th.jpg", "./images/work/psp_6th.jpg", "./images/work/psp_7th.jpg", "./images/work/psp_8th.jpg", "./images/work/psp_9th.jpg", "./images/work/intBE_top.jpg", "./images/work/intBE_2nd.jpg", "./images/work/intBE_3rd.jpg", "./images/work/intBE_4th.jpg", "./images/work/intBE_5th.jpg", "./images/work/sopho_top.jpg", "./images/work/sopho_2nd.jpg", "./images/work/sopho_3rd.jpg", "./images/work/prntBE_top.jpg", "./images/work/prntBE_2nd.jpg", "./images/work/prntBE_3rd.jpg", "./images/work/prntBE_4th.jpg", "./images/work/prntBE_5th.jpg", "./images/work/prntBE_6th.jpg", "./images/work/prntBE_7th.jpg", "./images/work/livelife_top.jpg", "./images/work/livelife_2nd.jpg", "./images/work/livelife_3rd.jpg", "./images/work/pointb_top.jpg", "./images/work/pointb_2nd.jpg", "./images/work/pointb_3rd.jpg");
	
});
