//Initiating jQuery to work with Prototype;
//var $j = jQuery.noConflict(); located inside jquery file

//Goes Document then window in the order you see below (it doesnt change if you rearrange)
$j(document).ready(function() { CoverFlow.onDocumentLoad(); });
$j(window).load(function() { CoverFlow.onWindowLoad(); });

var CoverFlow = {
	//global variables here
	//example : 'this is an example',
	onDocumentLoad : function(){
		try{
			$j("<div id='preload' style='display:none;'></div>").appendTo("#coverFlowImages");
			$j(coverFlowItems).each(function(){
				$j("<img />").appendTo("#preload").attr("src", this.imgurl);
			});
		} catch (e) {
			alert(e);
		}
	},

	onWindowLoad : function(){
		var docID = coverFlowDefault;
		var startID = CoverFlow.getCurrentDoc(docID);

		try{
			/*Gets the total Images in the array*/
			var totalImages = $j(coverFlowItems).length;
			
			/*Set the default docID*/
			//var docID = 1;
			//var startID = 0;

			/*Set variable to get the number of images on each side of center*/
			var maxImage = 6;
			var cfSpeed = 200;
			
			/*Check if Chrome use linkColor otherwise dont*/
			var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

			/*Set the reflection variables*/
			var reflectionHeight = 15;
			var reflectionDistance = 3;
			var reflectionTransparency = 50;

			$j('#coverFlowLeftArrow').append('<a href="javascript:void(0);"><<</a>').click(function(){
				 $j(".cfMiddle").prev().trigger('click');
			});

			$j('#coverFlowRightArrow').append('<a href="javascript:void(0);">>></a>').click(function(){
				$j(".cfMiddle").next().trigger('click');
			});

			$j(coverFlowItems).each(function(i){
				if(i>=parseInt(totalImages-maxImage)){
					$j('#coverFlowImages').append('<img src="'+this.imgurl+'" alt="'+this.title+'" title="'+this.title+'" class="cfLeft" id="image'+i+'" style="z-index:'+parseInt(i-totalImages+(maxImage+1))+'" />');
				}
			});

			$j(coverFlowItems).each(function(i){
				if(i==0){
					if(this.linkcolor && is_chrome){
						var linkColor='style="color:'+this.linkcolor+';"';
					}
					$j('#coverFlowImages').append('<img src="'+this.imgurl+'" alt="'+this.title+'" title="'+this.title+'" class="cfMiddle" id="image'+i+'" style="z-index:'+totalImages+'" />');
					$j('#coverFlowTitles').append('<a href="'+this.url+'" class="viewWindow" '+linkColor+'>'+this.title+'</a>');
					$j('#coverFlowMainContent').css("z-index",totalImages+2);
				} else if(i<=maxImage){
					$j('#coverFlowImages').append('<img src="'+this.imgurl+'" alt="'+this.title+'" title="'+this.title+'" class="cfRight" id="image'+i+'" style="z-index:'+parseInt(totalImages-i)+'" />');
				}
			});

			//remember that height, distance and transparency are all relative to the shadow
			//theres also border, color, and boxmode (boolean default is false)
			coverFlow_lib.add($j('#image0').get(0), { height: reflectionHeight, distance: reflectionDistance, transparency: reflectionTransparency-10, tilt: "none", iwidth: "200", iheight:"195" });

			$j('.cfLeft').each(function(){
				coverFlow_lib.add(this, { height: reflectionHeight, distance: reflectionDistance, transparency: reflectionTransparency, tilt: "right", iwidth: "200", iheight:"195" });
			});

			$j('.cfRight').each(function(){
				coverFlow_lib.add(this, { height: reflectionHeight, distance: reflectionDistance, transparency: reflectionTransparency, tilt: "left", iwidth: "200", iheight:"195" });
			});

			$j('.cfLeft').each(function(i){
				$j(this).css('opacity',(i==0)?'.40':(i==1)?'.60':(i==2)?'.80':'1');
			});

			$j('.cfRight').each(function(i){
				$j(this).css({'opacity':(i==5)?'.40':(i==4)?'.60':(i==3)?'.80':'1'});
			});

			//$j('*').click(function() { alert($j(this).attr('id')) });
			$j('canvas, VAR').livequery('click', function(event) {
				var className = $j(this).attr('class').replace(/ .*/, '');
				var currentID = $j(this).attr('id').replace("image","");
				var currentDoc = $j(coverFlowItems).get(currentID);

				var newLeftImageID = parseInt($j('.cfLeft:first').attr('id').replace('image',''))-1;
				newLeftImageID = (newLeftImageID==-1)?totalImages-1:newLeftImageID;
				var newLeftImage = $j(coverFlowItems).get(newLeftImageID);

				var newRightImageID = parseInt($j('.cfRight:last').attr('id').replace('image',''))+1;
				newRightImageID = (newRightImageID==totalImages)?0:newRightImageID;
				var newRightImage = $j(coverFlowItems).get(newRightImageID);

				//grab only the first classname in the class reference for switch
				switch(className){
					case 'cfLeft':
						if($j(this).next().hasClass("cfMiddle")){
							coverFlow_lib.modify(this, { tilt: 'none', transparency: reflectionTransparency-10 });
							if(currentDoc.linkcolor && is_chrome){
								var linkColor='style="color:'+currentDoc.linkcolor+';"';
							}
							$j('#coverFlowTitles').html('<a href="'+currentDoc.url+'" class="viewWindow" '+linkColor+'>'+currentDoc.title+'</a>');

							$j(this).removeClass('cfLeft').addClass('cfMiddle').css('z-index',totalImages);

							coverFlow_lib.modify($j(this).next('.cfMiddle').get(0), { tilt: 'left', transparency: reflectionTransparency });

							$j(this).next('.cfMiddle').removeClass('cfMiddle').addClass('cfRight');

							$j('.cfLeft').each(function(i){
								$j(this).css({'z-index':i+2,'opacity':(i==0)?'.60':(i==1)?'.80':'1'});
							});

							$j('.cfLeft:first').before('<img src="'+newLeftImage.imgurl+'" alt="'+newLeftImage.title+'" title="'+newLeftImage.title+'" class="cfLeft" id="image'+newLeftImageID+'" style="z-index:1" />');
							coverFlow_lib.add($j('#image'+newLeftImageID).get(0), { height: reflectionHeight, distance: reflectionDistance, transparency: reflectionTransparency, tilt: "right", iwidth: "200", iheight:"195" });
							$j('#image'+newLeftImageID).css({'opacity':'.40'});

							$j('.cfRight').each(function(i){
								$j(this).css({'z-index':(totalImages-1)-i,'opacity':(i==5)?'.40':(i==4)?'.60':(i==3)?'.80':'1'});
								if(i >= maxImage){
									$j(this).remove();
								}
							});

							CoverFlow.getCurrentFeaturedProject(currentDoc);
						} else {
							var totalMoves = parseInt(maxImage-$j(this).css('z-index'));
							for(var x = 0; x <=totalMoves; ++x) {
								setTimeout(function() {
									$j("#coverFlowLeftArrow").trigger('click');
								}, x * cfSpeed);
							}
						}
					break;
					case 'cfMiddle':
					break;
					case 'cfRight':
						if ( $j(this).prev().hasClass('cfMiddle') ){
							coverFlow_lib.modify(this, { tilt: 'none', transparency: reflectionTransparency-10 });
							if(currentDoc.linkcolor && is_chrome){
								var linkColor='style="color:'+currentDoc.linkcolor+';"';
							}
							$j('#coverFlowTitles').html('<a href="'+currentDoc.url+'" class="viewWindow" '+linkColor+'>'+currentDoc.title+'</a>');

							$j(this).removeClass('cfRight').addClass('cfMiddle').css('z-index',totalImages);

							coverFlow_lib.modify($j(this).prev('.cfMiddle').get(0), { tilt: 'right', transparency: reflectionTransparency });

							$j(this).prev('.cfMiddle').removeClass('cfMiddle').addClass('cfLeft');

							$j('.cfRight').each(function(i){
								$j(this).css({'z-index':parseInt(totalImages-i)-1,'opacity':(i==4)?'.60':(i==3)?'.80':'1'});
							});

							$j('.cfRight:last').after('<img src="'+newRightImage.imgurl+'" alt="'+newRightImage.title+'" title="'+newRightImage.title+'" class="cfRight" id="image'+newRightImageID+'" style="z-index:'+parseInt(totalImages-maxImage)+'" />');
							coverFlow_lib.add($j('#image'+newRightImageID).get(0), { height: reflectionHeight, distance: reflectionDistance, transparency: reflectionTransparency, tilt: "left", iwidth: "200", iheight:"195" });
							$j('#image'+newRightImageID).css({'opacity':'.40'});

							$j('.cfLeft').each(function(i){
								$j(this).css({'z-index':i,'opacity':(i==1)?'.40':(i==2)?'.60':(i==3)?'.80':'1'});
								if(i < 1){
									$j(this).remove();
								}
							});

							CoverFlow.getCurrentFeaturedProject(currentDoc);
						} else {
							var totalMoves = parseInt(totalImages-$j(this).css('z-index'));
							for(x=1;x<=totalMoves;x++){
								setTimeout(function() {
									$j("#coverFlowRightArrow").trigger('click');
								}, x * cfSpeed);
							}
						}
					break;
				}
			});

			CoverFlow.preloadFeaturedProject(totalImages, startID);

			CoverFlow.getCurrentFeaturedProject($j(coverFlowItems).get(0));

			setTimeout("CoverFlow.setIntroImageHoverHeight()",1000);
			
		} catch (e) {
			alert(e);
		}
	},

	preloadFeaturedProject:function(totalImages, startID){
		var setDirection = "#coverFlowRightArrow";
		var middleNumber = parseInt(Math.abs(totalImages/2));

		if(startID>middleNumber){
			startID = parseInt(totalImages-startID);
			setDirection = "#coverFlowLeftArrow";
		}

		for(x=1;x<=startID;x++){
			setTimeout(function() {
				$j(setDirection).trigger('click');
			}, x * 500);
		}
	},
	
	getCurrentFeaturedProject:function(currentDoc){
		var objImagePreloader = new Image();
		objImagePreloader.src = (currentDoc.imgurl).replace('Thumbs','Full_Page').replace('_thumb','')
		var intImageWidth = objImagePreloader.width+18;//18 is for the scrollbar
		var intImageHeight = objImagePreloader.height;
		var intHeight = parseInt($j(window).height()-40);
		var intMarginLeft = parseInt(intImageWidth/2);
		var featuredLeftMenu;
		var siteReloadURLBack;

		var leftSideTab = '<span class="side single"><b class="p1"></b><b class="p2"></b><b class="p3"></b><b class="p4 single"></b></span><span class="mid single">';
		var rightSideTab = '</span><span class="side single"><b class="p5"></b><b class="p6"></b><b class="p7"></b><b class="p8 single"></b></span>';

		//set left menu
		featuredLeftMenu = '<div id="introMenuTitle"><h3>Click to view:</h3></div>\
					<ul>\
						<li>\
							'+((currentDoc.weburl)?'<a href="'+currentDoc.weburl+'" title="'+currentDoc.title+'" class="viewWindow">Site Viewer</a>':'')+'\
						</li>\
						<li>\
							<a href="javascript:void(0);" class="sharethis">Share This</a>\
						</li>\
						<li>\
							<a href="'+objImagePreloader.src+'" class="fullScreen">Snapshot</a>\
						</li>\
						<li>\
							'+((currentDoc.weburl)?'<a href="'+currentDoc.weburl+'" title="'+currentDoc.title+'" class="website" target="_blank">Web Site</a>':'')+'\
						</li>\
						<li>\
							<a href="javascript:void(0);" class="support">Support</a>\
						</li>\
					</ul>';
//futureuse
//					<li>\
//						<a href="'+currentDoc.weburl+'" title="'+currentDoc.title+'" class="viewWindowAdmin">Admin Login</a>\
//					</li>\

		var newHTML = '<div id="introLeft">\
							<a href="'+objImagePreloader.src+'" class="fullScreen" id="introImageTitle">View Full Screen</a>\
							<a href="'+objImagePreloader.src+'" class="showcaseThumb fullScreen" id="introImage">\
								<img src="'+currentDoc.imgurl+'" alt="'+currentDoc.title+'" title="'+currentDoc.title+'" id="image_'+currentDoc.docID+'" class="showcaseIMG" />\
								<span id="introImageHover">\
									<img src="'+currentDoc.imgurl+'" alt="'+currentDoc.title+'" title="'+currentDoc.title+'" id="image_thumb_'+currentDoc.docID+'" />\
								</span>\
							</a>\
						</div>\
						<div id="introMiddle">\
							<h3><a href="'+currentDoc.url+'">'+currentDoc.title+'</a></h3>\
							<p>'+currentDoc.introtext+'</p>\
						</div>\
						<div id="introRight">'+featuredLeftMenu+'</div>';

		$j('#coverFlowShowcase').html(newHTML);

		$j('.support').click(function(e){
			$j('#mainMenu a.contact').trigger('click');
			$j('#subject').val("Support request");
		});

		$j(".sharethis").click(function(e){
			$j('#sharethis_0 a').trigger("click");
		});

		$j('.viewWindow, .fullScreen, .viewWindowAdmin').click(function(e){
			e.preventDefault();

			objImagePreloader.src = (currentDoc.imgurl).replace('Thumbs','Full_Page').replace('_thumb','');
			intImageWidth = objImagePreloader.width+25;//25 is for the scrollbar
			intImageHeight = objImagePreloader.height;
			intHeight = parseInt($j(window).height()-40);//-40 for the top tab buffer
			myAdjustableHeight = Math.min(parseInt(intHeight),parseInt(intImageHeight));
			intMarginLeft = Math.abs(intImageWidth/2)*-1;

			if($j(this).hasClass("fullScreen")){
				$j('<div id="innerWindow"><img src="'+objImagePreloader.src+'" /></div>').modal();
			} else {
				switch(currentDoc.mantype){
					case 'manager':
						siteReloadURLBack = currentDoc.weburl+"/manager/";
					break;
					case 'administrator':
						siteReloadURLBack = currentDoc.weburl+"/administrator/";
					break;
					default:
						siteReloadURLBack = currentDoc.weburl;
					break;
				}

				if ($j(this).hasClass("viewWindowAdmin")){
					var viewWindowFront = 'style="display:none;"';
					var viewWindowBackActive = 'class="active"';
				} else {
					var viewWindowBack = 'style="display:none;"';
					var viewWindowFrontActive = 'class="active"';
				}
//<li '+viewWindowBackActive+'><a href="javascript:void(0);" id="sitebackend">'+leftSideTab+'backend'+rightSideTab+'</a></li>\
				var innerHTML = '<div id="tabs">\
									<div id="tabsLeft">\
										<ul class="tabMenu">\
											<li '+viewWindowFrontActive+'><a href="javascript:void(0);" id="sitefrontend" class="modalReload">'+leftSideTab+'home'+rightSideTab+'</a></li>\
											<li><a href="javascript:void(0);" id="sitereload">'+leftSideTab+'reload'+rightSideTab+'</a></li>\
											<li><a href="javascript:void(0);" id="sitesupport">'+leftSideTab+'support'+rightSideTab+'</a></li>\
										</ul>\
									</div>\
									<div id="tabsRight">\
										<ul class="tabMenu">\
											<li><a href="'+currentDoc.weburl+'" target="_blank" title="click to visit \"'+currentDoc.title+'\" in new window">'+leftSideTab+currentDoc.title+rightSideTab+'</a></li>\
										</ul>\
									</div>\
								</div><div id="innerWindow" style="overflow:hidden;">\
									<div id="frontEnd" '+viewWindowFront+'><iframe src="'+currentDoc.weburl+'" title="'+currentDoc.title+'" name="viewWindowFront" id="viewWindowFront" frameborder="0" width="'+intImageWidth+'" height="'+myAdjustableHeight+'" /></div>\
								</div>';
//<div id="backEnd" '+viewWindowBack+'><iframe src="'+siteReloadURLBack+'" title="'+currentDoc.title+' admin" name="viewWindowBack" id="viewWindowBack" frameborder="0" width="'+intImageWidth+'" height="'+myAdjustableHeight+'" /></div>\
				$j(innerHTML).modal();
			}

			$j('#modalContainer').css({'width': intImageWidth, 'height': myAdjustableHeight, 'margin-left': Math.floor(intMarginLeft)});
			$j('#innerWindow').css({'width': intImageWidth, 'height': myAdjustableHeight});
			$j('#tabs').css({'width': intImageWidth+2, 'height': 20, 'margin-left': Math.floor(intMarginLeft)-1});

			$j('#sitefrontend').click(function(e){
				$j(this).parent().addClass("active");
				$j('#sitebackend').parent().removeClass("active");
				$j('#frontEnd').css('display','inline');
				$j('#backEnd').css('display','none');
//				$j('#backEnd').slideUp("10000",function(){
//					$j('#frontEnd').slideDown("10000",function(){
//						//$j(this).css("display","inline");
//					});
//				});
			});

			$j('#sitebackend').click(function(e){
				$j(this).parent().addClass("active");
				$j('#sitefrontend').parent().removeClass("active");
				$j('#frontEnd').css('display','none');
				$j('#backEnd').css('display','inline');
//				$j('#frontEnd').slideUp("10000",function(){
//					$j('#backEnd').slideDown("10000",function(){
//						//$j(this).css("display","inline");
//					});
//				});
			});

			$j('#sitereload').click(function(e){
				//clientIP is inline script on the site
				var activeTab = ($j('#frontEnd').css('display')=='inline')?'front':'back';
				var siteReloadURL = $j("#viewWindowFront").attr("src");

				switch(currentDoc.mantype){
					case 'manager':
						siteReloadURL = currentDoc.weburl+"/assets/plugins/lycosideaPortal/clientJS/"+clientIP+".js";
					break;
					case 'administrator':
						siteReloadURL = currentDoc.weburl+"/modules/lycosideaPortal/clientJS/"+clientIP+".js";
					break;
					default:
						siteReloadURL = $j("#viewWindowFront").attr("src");
					break;
				}

				if(currentDoc.mantype&&activeTab){
					$j.ajax({
						type: "GET",
						dataType: "script",
						url: siteReloadURL,
						success: function(){
							loc = (typeof(loc) == 'undefined')?$j("#viewWindowFront").attr("src"):loc;
							$j("#viewWindowFront").attr("src",loc);
						}
					});
				} else {
					if(activeTab=='back'){
						siteReloadURL = siteReloadURLBack;
					}
					$j("#viewWindowFront").attr("src",siteReloadURL);
				}
			});

			$j('#sitesupport').click(function(e){
				$j('#mainMenu a.contact').trigger('click');
				$j('#subject').val("Support request");
			});
		});

		CoverFlow.setIntroImageHoverHeight();
	},
	
	getCurrentDoc:function(docID){
		var returnID = 0;
		$j(coverFlowItems).each(function(i){
			if(this.docID==docID){
				returnID = i;
				return false;
			}
		});
		return returnID;
	},

	setIntroImageHoverHeight:function(){
		//Update the Hover position.
		var introImageHoverHeight = $j('#introImageHover img').height();

		$j('#introImageHover').css("top",parseInt(-(introImageHoverHeight /2)+55));
	}
}