var defaultColor;
var sPage;
$(document).ready(function(){
	$("#angle-shots ul li img").click( function(){
		swapMainImageView(this);
	})
	
	$("#frame-color h3, #frame-color img").click( function(){
		swapMainImageColor(this);
	})
	var sPath = window.location.pathname;
	//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
	sPage = sPath.substring(sPath.lastIndexOf('/') + 1).replace('.php', '');

	defaultColor = $("#main-photos img:first").attr('src').replace( '../assets/images/eyewear/' + sPage + '/main-', '' ).replace( '-front.jpg', '' );
	
	/* add to cart script */
	$("form").submit(function(){
		var parameter = $("#select-frame-color").val()+" - "+$("#select-lens-color").val().replace("ReActive ", "");
		$("input[name='os0']").val(parameter);
	});
	
	/* hover effects */
	$(".shopping-cart, #collection, #mug-shots, #lens-options, #temple-fit, #case, #model-info").hover(
		function(){
			$(this).toggleClass("highlight");
		},
		function(){
			$(this).toggleClass("highlight");
		}
	);
	$(".popup-close").hover(
		function(){
			$(".popup-close img").attr("src", "../assets/images/global/close-button-hover.png");
		},
		function(){
			$(".popup-close img").attr("src", "../assets/images/global/close-button.png");
		}
	).click(function(event){event.preventDefault()});
});


/* functions for swapping main image */
function swapMainImageView(triggerImage){
	var targetSource = triggerImage.getAttribute('src').replace('angle-shots', 'main-' + defaultColor);
	/* cuz ie sucks */
	targetSource = targetSource.replace('http://' + document.domain + '/GINoptics', '..');
	$("#main-photos .show, #color-lens .show").toggleClass("show");
	$("#main-photos img[src='"+targetSource+"']").toggleClass("show");
	
	$("#color-lens p:contains('"  + defaultColor.substring(0,1).toUpperCase() + "'):first").toggleClass("show");
	
	var index;
	var paras = $("#color p");
	for(i=0; i<paras.length; i++){
		if(paras[i].className == "show"){
			index = i;
		}
	}
	$("#lens p:eq(" + index + ")").toggleClass("show");
}

function swapMainImageColor(trigger){
	var color;
	var targetSource;
	if(trigger.getAttribute('src')){
		color = trigger.getAttribute('src');
		/* cuz ie sucks */
		color = color.replace('http://' + document.domain + '/GINoptics', '..');
		color = color.replace('../assets/images/eyewear/' + sPage + '/frame-colors-', '').replace('.jpg', '');
		targetSource = trigger.getAttribute('src').replace('frame-colors', 'main').replace('.jpg', '-side.jpg');
	} else {
		color = trigger.className.replace("frame-color-", "")
		targetSource = '../assets/images/eyewear/' + sPage + '/main-' + color + "-side.jpg"
	}
	color=color.replace('-', ' ');

	color = ucwords(color);
	color=color.replace(' ', '/');
	/* cuz ie sucks */
	targetSource = targetSource.replace('http://' + document.domain + '/GINoptics', '..');
	$("#main-photos .show").toggleClass("show");
	$("#main-photos img[src='" + targetSource + "'], #color-lens p:contains('"  + color + "')").toggleClass("show");
	$("#color-lens .show").toggleClass("show");

	$("#color-lens p:contains('"  + color + "'):first").toggleClass("show");
	
	var index;
	var paras = $("#color p");
	for(i=0; i<paras.length; i++){
		if(paras[i].className == "show"){
			index = i;
		}
	}
	$("#lens p:eq(" + index + ")").toggleClass("show");
}

function ucwords( str ) {
    return (str+'').replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } );
}

/* shadowbox script */
$(document).ready(function(){
	$(".popup").css('display', 'none');
	$("#collection, #mug-shots, #lens-options, #temple-fit, #case, #model-info, #shopping-cart, #action").click(
		function () {
			var ID = $(this).attr('id');
			centerPopup(ID);
			loadPopup(ID);
		}
	);

	$(".popup-close").click(function(){
		disablePopup();
	});

	$("#popup-background").click(function(){
		disablePopup();
	});

	$(document).keypress(
		function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		}
	);
});

var popupStatus = 0;

function loadPopup(ID){
	if(popupStatus==0){
		$("#popup-background").css({
			"opacity": "0.7"
		});
		$("#popup-background").fadeIn("slow");
		$("." + ID + "-popup").fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#popup-background").fadeOut("slow");
		$(".popup").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(ID) {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("." + ID + "-popup").height();
	var popupWidth = $("." + ID + "-popup").width();
	
	$("." + ID + "-popup").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	$("#popup-background").css({
		"height": windowHeight
	});
}