// JavaScript Document

//constants and globals
var pageWidth = 1600;
var thumbInfo;
var maxStack = 400;
var mosaicHeight = 0;
var currentHighlightBox = -1;
var highlightBoxTop;
var currentBox;
var displayState;
var infoOpen = false;
var currentLargeImage;
var largeImgInfoOpen;
var headerReadyBool = false;
var moscaicReadyBool = false;
var mosaicInitDraw = false;
var infoNavCurrent = 1;

var noEnlargeList = new Array (1,102);

function findCaller(e)
{
var targ;
if (!e)
  {
  var e=window.event;
  }
if (e.target)
  {
  targ=e.target;
  }
else if (e.srcElement)
  {
  targ=e.srcElement;
  }
if (targ.nodeType==3) // defeat Safari bug
  {
  targ = targ.parentNode;
  }
return targ;
}

function ismobilesafari() {
    if( navigator.userAgent.match( /(iPod|iPhone|iPad)/ ) ) {
        return true
    } else {
        return false
    }
}

function GetWidth() {
	returnWidth = $(window).width();
	if(returnWidth < 800) {
		returnWidth = 800;					   
	}
	return returnWidth;
}

function queryString(searchString) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == searchString) {
			return ft[1];
		}
	}
}



function setPage () {
}

function initMosaic() {

	thumbNum = 1;
	thumbInfo = new Array();
	thumb_id = "thumb"+thumbNum;
	while(thumbObject = document.getElementById(thumb_id)) {
		thumbInfo[thumbNum] = new Array();
		thumbInfo[thumbNum]['width'] = parseInt(thumbObject.style.width);
		thumbInfo[thumbNum]['height'] = parseInt(thumbObject.style.height);
		thumbInfo[thumbNum]['left'] = parseInt(thumbObject.style.left);
		thumbInfo[thumbNum]['top'] = parseInt(thumbObject.style.top);
		thumbInfo[thumbNum]['right'] = parseInt(thumbObject.style.right);
		thumbInfo[thumbNum]['title'] = thumbObject.title;
		thumbObject.title = "";
		innerDiv1 = '<div id="thumb'+thumbNum+'Inner" >';
		innerDiv2 = '</div>';
		thumbObject.innerHTML = innerDiv1 + thumbObject.innerHTML + innerDiv2;
		thumbIObject = document.getElementById("thumb"+thumbNum+"Inner");
//		thumbIObject.style.display = "none";
		thumbNum++;
		thumb_id = "thumb"+thumbNum;
	}
	//set display state to mosaic for resize
	displayState = 1;
	moscaicReadyBool=true;
	InitMosaicTimer();

}

function drawMosaic () {
	
	pageScale = GetWidth() / pageWidth;
//	alert("page scale: "+pageScale);
	thumbNum = 1;
	mosaicHeight = 0;
	thumb_id = "thumb"+thumbNum;
	thumb_i_id = "thumb"+thumbNum+"Inner"
	while(thumbObject = document.getElementById(thumb_id)) {
		thumbWidth = Math.ceil((thumbInfo[thumbNum]["width"])*pageScale)-1;
		thumbHeight = Math.ceil((thumbInfo[thumbNum]["height"])*pageScale)-1;
		$("#"+thumb_id).css("width",thumbWidth);
		$("#"+thumb_i_id).css("width",thumbWidth+2);
		$("#"+thumb_id).css("height",thumbHeight);
		$("#"+thumb_i_id).css("height",thumbHeight+2);
		thumbTop = Math.ceil(thumbInfo[thumbNum]["top"]*pageScale)-1;
		$("#"+thumb_id).css("top",thumbTop);
//		var randColor = '#'+Math.floor(Math.random()*16777215).toString(16);
//		$("#"+thumb_id).css("border-color",randColor);
		if(!isNaN(thumbInfo[thumbNum]["left"])) {
			thumbLeft = Math.ceil((thumbInfo[thumbNum]["left"])*pageScale)-1;
			$("#"+thumb_id).css("left",thumbLeft);
		} else {
			thumbRight = Math.ceil(thumbInfo[thumbNum]["right"]*pageScale)-1;
			if(thumbRight <0) {
				thumbRight = 0;
				$("#"+thumb_id).css("width",thumbWidth-1);
			};
			$("#"+thumb_id).css("right",thumbRight);
		}
		if(thumbHeight + thumbTop > mosaicHeight) {
			mosaicHeight = thumbHeight + thumbTop;
		}
		thumbNum++;
		thumb_id = "thumb"+thumbNum;
		thumb_i_id = "thumb"+thumbNum+"Inner"
	}
	$("#mosaic").css("height", mosaicHeight);
	
	if(!mosaicInitDraw) {
		mosaicInitDraw = true;
		$('#mosaic').fadeIn(2000);
	}
	
}

function drawHeader() {
	pageScale = GetWidth() / pageWidth;
	//alert("page scale at draw header: "+pageScale);
	headerHeight = Math.floor(62*pageScale);
	if(headerHeight < 31) {
		headerHeight = 31;
	}
	$("#mainStripe").css("height",headerHeight);
	$("#mosaic").css("margin-top",headerHeight);
	$("#mainStripe").fadeIn(100);
	headerReadyBool = true;
	InitMosaicTimer();
}

function highlightBox(boxNum) {
//	$("#selectBox").css({ opacity: 0.0 });

	$("#noEnlargeBox").hide();

	$("#selectBox").hide();

//	$("#selectBox").html(Math.floor(Math.random()*50));	
	thumb_id = "thumb"+boxNum;
	thumbObject = document.getElementById(thumb_id)
	boxWidth = parseInt(thumbObject.style.width);
	boxHeight = parseInt(thumbObject.style.height);
	
	if( !ismobilesafari() ) {
		boxTop = parseFloat(thumbObject.style.top) + parseFloat($("#mosaic").css("margin-top")) - $(window).scrollTop()+3;
	} else {
		boxTop = parseFloat(thumbObject.style.top) + parseFloat($("#mosaic").css("margin-top")) + 3;
	}
	highlightBoxTop = parseFloat(thumbObject.style.top) + parseFloat($("#mosaic").css("margin-top"))+3;
	$("#selectBox").css("border-width", 5);
	$("#selectBox").css("width", boxWidth-8);
	$("#selectBox").css("height", boxHeight-8);
	$("#selectBox").css("top", boxTop);
	//alert( thumbInfo[boxNum]['title']);
	$("#selectBox").attr("title", thumbInfo[boxNum]['title']);
	if(!isNaN(thumbInfo[boxNum]["left"])) {
		boxLeft = thumbObject.style.left;
		$("#selectBox").css("left", boxLeft);
		$("#selectBox").css("right", "");
	} else {
		boxRight = thumbObject.style.right;
		$("#selectBox").css("left", "");
		$("#selectBox").css("right", boxRight);
	}
	
	currentBox = boxNum;
	$("#selectBox").fadeIn(300);
}

$("#mosaic").ready(function() {
});

function InitMosaicTimer () {
	if(moscaicReadyBool && headerReadyBool && !mosaicInitDraw) {
		drawMosaic();
	}
}


function loadImg2 (currentBox, animFlag) {
//	alert("word");
	pageScale = GetWidth() / pageWidth;
//	alert("page scale2: "+pageScale);

	if(animFlag != false) {
		//expand image with animation
		
		closeImgInfo();
		$("#enlargeBox").css("display", "block");
		$("#enlargeBox").css("top", Math.ceil(thumbInfo[currentBox]['top']*pageScale)-1);
		$("#enlargeBox").css("width", Math.ceil(thumbInfo[currentBox]['width']*pageScale)-1);
		$("#enlargeBox").css("height", Math.ceil(thumbInfo[currentBox]['height']*pageScale)-1);
		$("#enlargeBox").css("left", Math.ceil(thumbInfo[currentBox]['left']*pageScale)-1);
	}

	//$("#thumb"+currentBox+" .expandInfo").css("display", "block");

	expandLeft = parseInt($("#enlargeSizeDiv").data("photo"+currentBox).left)-2;
	expandTop = parseInt($("#enlargeSizeDiv").data("photo"+currentBox).top)-2;
	expandWidth = parseInt($("#enlargeSizeDiv").data("photo"+currentBox).width)-1;
	expandHeight = parseInt($("#enlargeSizeDiv").data("photo"+currentBox).height)-1;

	//alert ("expand dim left: "+$("#enlargeSizeDiv").data("photo"+currentBox).left);
	//alert ("thumb box id: "+thumbInfo[currentBox]['left']);

	//$("#thumb"+currentBox+" .expandInfo").css("display", "none");

	expandLeft = Math.ceil(expandLeft*pageScale);
	expandTop = Math.ceil(expandTop*pageScale);
	expandWidth = Math.ceil(expandWidth*pageScale);
	expandHeight = Math.ceil(expandHeight*pageScale);

	$("#enlargeBox img").remove();

	var largeImgLoadFlag = false;
	var largeImgAnimFlag = false;
	var img = new Image();
	
	$(img).attr("src", "images/thumbs/rm_"+currentBox+".jpg");
	$(img).attr("id", "thumbStretched");
	$("#enlargeBox").show();
	$(img).css("width", $("#thumb"+currentBox).css("width"));
	$(img).css("height", $("#thumb"+currentBox).css("height"));
	
	$(img).animate( {
		width: expandWidth,
		height: expandHeight
	}, 500 );
//	$(img).css("width", expandWidth);
//	$(img).css("height", expandHeight);

	$("#enlargeBox").append($(img));

	$("#largeImgBorder").hide();
	
	$("#enlargeBoxSpinner").fadeIn(100);

	if(animFlag == false) {
		$("#enlargeBox").css("top", expandTop);
		$("#enlargeBox").css("width", expandWidth);
		$("#enlargeBox").css("height", expandHeight);
		$("#enlargeBox").css("left", expandLeft);

		$("#largeImgBorder").css("width", expandWidth-4);
		$("#largeImgBorder").css("height", expandHeight-4);
		$("#largeImgBorder").show();
	}
	else {
		$("#enlargeBox").animate( {
			left: expandLeft,
			top: expandTop,
			width: expandWidth,
			height: expandHeight
		 }, 500, function () {
			$("#largeImgNav").show(); 
			$("#largeImgBorder").css("width", expandWidth-4);
			$("#largeImgBorder").css("height", expandHeight-4);
			$("#largeImgBorder").show();
			if(largeImgLoadFlag == true) {
				$("#largeImgHighRes").fadeIn(200);
				largeImgAnimFlag = false;
			} else {
				largeImgAnimFlag = true;
			}
		 });
	}

	imgString = "images/bigImgs/rm_"+currentBox+'.jpg';

	var imgLarge = new Image();
	$(imgLarge).attr("id", "largeImgHighRes")
	$(imgLarge).load( function () {

//		$(imgLarge).hide();
		$("#enlargeBoxSpinner").hide();
		$(this).css('display', 'none');
		$("#enlargeBoxLargeImg").append(imgLarge);
//		alert($(imgLarge).width);
		$(imgLarge).css("width", expandWidth);
		$(imgLarge).css("height", expandHeight);
		
		if(largeImgAnimFlag == true) {
			$(imgLarge).fadeIn(200);
			largeImgLoadFlag = false;
		} else {
			largeImgLoadFlag = true;
		}

	}).error(function () { alert("img load error");}).attr("src", imgString);
	
	currentLargeImage = currentBox;
	displayState = 2;

}

function closeLargeImg() {

	pageScale = GetWidth() / pageWidth;

	expandTop = Math.ceil(thumbInfo[currentLargeImage]['top']*pageScale)-1;
	expandWidth = Math.ceil(thumbInfo[currentLargeImage]['width']*pageScale)-1;
	expandHeight = Math.ceil(thumbInfo[currentLargeImage]['height']*pageScale)-1;

	closeImgInfo();
	$("#largeImgBorder").hide();

//	$("#thumbStretched").css("width", $("#thumb"+currentBox).css("width"));
//	$("#thumbStretched").css("height", $("#thumb"+currentBox).css("height"));
	
	//getImgId
	boxNum = parseInt($("#thumbStretched").attr("src").split("_")[1]);
//	alert(boxNum);
	
	contractWidth = $("#thumb"+boxNum).css("width");
	contractHeight = $("#thumb"+boxNum).css("height");
	
	$("#thumbStretched").animate( {
		width: contractWidth,
		height: contractHeight
	}, 500 );


	expandLeft = Math.ceil(thumbInfo[currentLargeImage]['left']*pageScale)-1;
	expandRight = null;
	$("#enlargeBox").animate( {
		left: expandLeft,
		top: expandTop,
		width: expandWidth,
		height: expandHeight
	 }, 500, function () {	$("#enlargeBox").hide(); });

	$("#largeImgHighRes").fadeOut(10);
	$("#largeImgNav").hide();
	displayState = 1;

}


function showInfo(animateFlag) {
	
	localWidth = GetWidth();
	if(GetWidth() > 1920) {
		localWidth = 1920;
	}
	
	stageScale = localWidth / 800;
	
	$("#infoWrapper").css("margin-top", Math.round(parseInt($("#mainStripe").css("height"))+3));
	$("#infoArea div").css("font-size", Math.floor(13*Math.pow(stageScale, 0.6))+"px");
//	$("#infoArea div").css("letter-spacing", 0.1*(stageScale-1)+"em");
	$("#infoArea div").css("line-height", Math.floor(20*Math.pow(stageScale, 0.6))+"px");
	$(".contactHeader").css("font-size", Math.floor(16*Math.pow(stageScale, 0.4))+"px");
	$(".contactHeader").css("line-height", Math.floor(16*Math.pow(stageScale, 0.4))+"px");
	$(".contactHeader").css("margin-top", Math.floor(5*Math.pow(1/stageScale, 0.6))+"%");
	$(".contactHeader").css("letter-spacing", "0.1em");
	$("#contactAddress").css("line-height", Math.floor(16*Math.pow(stageScale, 0.6))+"px");
	$("#facebookLikeBox").css("top", Math.floor(-70*(1+(stageScale-1)/7))+"px");

	$("#closeInfo").css("right", Math.floor(10*(1+(stageScale-1)/.6))+"%");
	
	$("#navText").css("margin-top", (localWidth*3/100)+"px");
	
	$("#infoText2").css("font-size", Math.floor(12*Math.pow(stageScale, 0.7))+"px");
	$(".infoList").css("line-height", Math.floor(15*Math.pow(stageScale, 0.7))+"px");
	$(".infoList p").css("margin-bottom", Math.floor(8*Math.pow(stageScale, 0.4))+"px");
	$(".infoList p").css("margin-top", Math.floor(8*Math.pow(stageScale, 0.4))+"px");
	
	
//	fieldHeight = $("#infoText").height() + GetWidth() * 0.1;
	if(animateFlag == false) {
		$("#infoWrapper").css("height", 380*Math.pow(stageScale/1.1, 0.4)-2);
	} else {
		$("#infoWrapper").animate( {
			height: 380*Math.pow(stageScale/1.1, 0.4)-2,
		}, 300, function () { $("#infoArea").fadeIn(300)} );
		$("#infoBGdiv").fadeTo (0, 0.96);
		$("#infoImg").attr("src", "images/topLogo2b2-over.gif");
	}
	infoOpen = true;
}

function hideInfo(animateFlag) {
	
	$("#infoWrapper").animate( {
		height: 0,
	}, 300, function () { 
		$("#infoArea").hide(); 	
		$("#infoBGdiv").fadeTo (0, 0.0);
		$("#infoWrapper").css("margin-top", 0);
	}  );
	
	$("#infoImg").attr("src", "images/topLogo2b2.gif");

	infoOpen = false;
}

function showImgInfo () {

	stageScale = GetWidth() / 800;

	window.setTimeout( function () {	largeImgInfoOpen = true;},800 );

	$("#largeImgInfoText").css("font-size", Math.floor(Math.pow(stageScale, 0.6)*10));
	$("#largeImgInfoText").empty();

	$("#largeImgInfoText").append(thumbInfo[currentLargeImage]['title']);
	$("#largeImgInfo").show();
	
	
}

function closeImgInfo () {
	largeImgInfoOpen = false;
	$("#largeImgInfoText").empty();
	$("#largeImgInfo").hide();
}

function noEnlargeShowInfo (boxNum) {
	
	if(displayState == 2) { return; }
	
	$("#selectBox").hide();

	stageScale = GetWidth() / 800;

	noEnlargeImgInfoOpen = true;

	$("#noEnlargeBox").css("width", parseInt($("#thumb"+boxNum).css("width"))+1+"px");
	$("#noEnlargeBox").css("height", $("#thumb"+boxNum).css("height"));
	$("#noEnlargeBox").css("left", $("#thumb"+boxNum).css("left"));
	$("#noEnlargeBox").css("top", $("#thumb"+boxNum).css("top"));


	$("#noEnlargeImgInfoText").css("font-size", Math.floor(Math.pow(stageScale, 0.6)*10));
	$("#noEnlargeImgInfoText").css("text-align", "right");
	$("#noEnlargeImgInfoText").empty();

	$("#noEnlargeImgInfoText").append(thumbInfo[boxNum]['title']);
	$("#noEnlargeBox").fadeIn(200);

}



$(document).ready (function () {
	setEnlargeSizes();
	drawHeader();

});

$(window).scroll(function() {
	if( !ismobilesafari() ) {
		$("#selectBox").css("top", highlightBoxTop - $(window).scrollTop());
	}
	
});


$(window).resize(function() {
  	$("#selectBox").hide();
	$("#noEnlargeBox").hide();
	//alert($("#mosaic").width());
	if($(window).width() < 800) {
		$("#mainStripe").width(800)
		$("#mosaic").width(800)
	} else {
		$("#mainStripe").width($(window).width())
		$("#mosaic").width($(window).width())
	}
   drawHeader();
   drawMosaic();
   if(displayState == 2) {
	   loadImg2(currentLargeImage, false);
	   if(largeImgInfoOpen) {
		   showImgInfo();
	   }
   }
   if(infoOpen) {
	   showInfo(false);
   }
});

$(window).load( function () {
	$("#mosaic").css("height", ($(window).width() / 1.4));
	initMosaic();


	for( var j in noEnlargeList) {
		$("#thumb"+noEnlargeList[j]+" > div > img").css("cursor", "default");
	}



	$(".thumbImg").mouseenter( function () {
										 
		imgSrc = $(this).attr("src");
		imgSplit1 = imgSrc.split("_");
		imgSplit2 = imgSplit1[1].split(".");;
		boxNum = parseInt(imgSplit2[0]);
		if(currentHighlightBox != boxNum) {
//			$("#selectBox").html(Math.floor(Math.random()*50));

			if($.inArray(boxNum, noEnlargeList)>-1) {
				noEnlargeShowInfo(boxNum);
			} else {
				highlightBox(boxNum);
			}
			currentHighlightBox = boxNum;
		}
		
	 });

	$(".thumbImg").click( function () {
										 
		$("#selectBox").css("display", "none");
		//$(".thumbImg").unbind("mouseenter")
	//	alert("invoked single tap");
		loadImg2(currentBox);
		
	 });

	
	$("#selectBox").click( function () {
		
		$("#selectBox").css("display", "none");
		//$(".thumbImg").unbind("mouseenter")
	//	alert("invoked single tap");
		loadImg2(currentBox);

	});
		
	
	$("#mainStripe").mouseenter ( function () {
		currentHighlightBox = -1;
		$("#selectBox").css("display", "none");
	});
	
	$("#infoImg").mouseover ( function () {
		$(this).attr("src", "images/topLogo2b2-over.gif");
	});
	$("#infoImg").mouseout ( function () {
	   if(!infoOpen) {
			$(this).attr("src", "images/topLogo2b2.gif");
	   }
	});
	$("#infoImg").click ( function () {
		if(!infoOpen) {
			showInfo();
		} else {
			hideInfo();
		}
 	});
	
	$("#closeInfo").mouseover ( function () {
		$(this).css("color", "#0242a5");
	});
	$("#closeInfo").mouseout ( function () {
		$(this).css("color", "#335291");
	});
	$("#closeInfo").click ( function () {
		hideInfo();
 	});
	
	$("#largeImgX").fadeTo(0,0.6);
	$("#largeImgQ").fadeTo(0,0.6);
	
	$("#largeImgX").mouseover ( function () {
		$("#largeImgX").fadeTo(0,0.9);
	});
	$("#largeImgX").mouseout ( function () {
		$("#largeImgX").fadeTo(0,0.6);
	});

	$("#largeImgQ").mouseover ( function () {
		if (largeImgInfoOpen != true) {
			showImgInfo();
		}
		$("#largeImgQ").fadeTo(0,0.9);
	});
	$("#largeImgQ").mouseout ( function () {
		$("#largeImgQ").fadeTo(0,0.6);
	});

	$("#largeImgX").click ( function () {
		closeLargeImg();
 	});

	$("#largeImgQ").click ( function () {
		if (largeImgInfoOpen != true) {
			showImgInfo();
		} else {
			closeImgInfo();
		}
 	});

	//set up info nav
	$("#infoNav_"+infoNavCurrent).css("color", "#2d2d2d");

	j=1;
	while($("#infoNav_"+j).length > 0) {
		$("#infoNav_"+j).css("cursor", "pointer");
		$("#infoNav_"+j).mouseover ( function () {
			$(this).css("color", "#2d2d2d");
		});
		$("#infoNav_"+j).mouseout ( function () {
			imgSplit1 = $(this).attr("id").split("_");
			navID = parseInt(imgSplit1[1]);
			if( navID != infoNavCurrent) {
				$(this).css("color", "#666666");
			}
		});
		
		if(j <= 2) {
			$("#infoNav_"+j).click ( function () {
				imgSplit1 = $(this).attr("id").split("_");
				navID = parseInt(imgSplit1[1]);
				if( navID != infoNavCurrent) {
					$("#infoText"+infoNavCurrent).fadeOut(100, function () {
						k=1;
						while($("#infoNav_"+k).length > 0) {
							if(k != navID) {
								$("#infoNav_"+k).css("color", "#666666");
							}
							k++;
						}
						$("#infoText"+navID).fadeIn(400);
						infoNavCurrent = navID;
					});
				}
			});
		}
		j++;
	}

	$("#infoNav_3").click(function () {
		window.open("http://www.razummedia.com/rm_brochure.pdf", '_blank');
 	});

} );

