﻿$(document).ready(function () {
    //bind thumbnail click
    $(".imgFeatureThumb, .imgFeatureThumbX").click(function () {
        //clear other selections
        $(".aFeatureThumbX").removeClass('selected');

        //add selection
        $(this).parent().addClass('selected');

        //set large image
        setHero($(this));
    })


    //===========================================================
    //Prev Button
    //===========================================================
    //left arrow key
    $(document).keydown(function (e) {
        if (e.keyCode == 37) {
            $("[alt_id='btnImgPrev']").click();
        }
    });

    $("[alt_id='btnImgPrev']").click(function () {
        //get current selection
        oCurrent = $(".aFeatureThumbX.selected").first()

        if ($(oCurrent).prev().length > 0) {
            oPrev = $(oCurrent).prev();
        } else {
            oPrev = $(".aFeatureThumbX").last();
        }

        $(oCurrent).removeClass('selected');
        $(oPrev).addClass('selected');
        $(".scrlOrangeBoxX").scrollTo($(oPrev));
        setHero($(oPrev).children("img"));
    });

    //===========================================================
    //Next Button
    //===========================================================
    //right arrow key
    $(document).keydown(function (e) {
        if (e.keyCode == 39) {
            $("[alt_id='btnImgNext']").click();
        }
    });

    $("[alt_id='btnImgNext']").click(function () {
        //get current selection
        oCurrent = $(".aFeatureThumbX.selected").first()

        if ($(oCurrent).next().length > 0) {
            oNext = $(oCurrent).next();
        } else {
            oNext = $(".aFeatureThumbX").first();
        }

        $(oCurrent).removeClass('selected');
        $(oNext).addClass('selected');        
        $(".scrlOrangeBoxX").scrollTo($(oNext));
        setHero($(oNext).children("img"));
    });




    //preload images - dynamically
    imagePreLd = new Image();
    $(".imgFeatureThumb, .imgFeatureThumbX").each(function () {
        //does not work in IE for preloading: jQuery("<img />").attr("src", $(this).attr("alt_xlgsrc"));
        imagePreLd.src = $(this).attr("alt_lgsrc");
    });

    //	$(".imgFeatureThumb").unload(function() {
    //		$(this).fadeIn(2000);
    //	});


    //initialize popup
    initPop();

    //bind popup toggle
    bindPopupToggle($("[alt_id='imgLarge']"));

    //set how anchor on zoomer behave prior to zooming.
    killZoomer();

    $("#enableZoom").click(function () { toggleZoom($(this)) });

});

//**********************************************************************************
//** bind popup toggle
function bindPopupToggle(jQO) {
    var img = jQO;  //$("[alt_id='imgLarge']");

	//unbind anchor
	$("a#aOpenXL").unbind();

	//if no larger image then show no image
	if (img.prop("alt_xlgsrc") == "" || img.prop("alt_xlgsrc") == "/images/spacer.gif") {
	    $("a#aOpenXL").attr("title","");
		$("a#aOpenXL").removeAttr("href");

    } else {
        $("a#aOpenXL").attr("title","See Larger Image");
        $("a#aOpenXL").attr("href", window.location + "#dvXLargeImage");

        //bind fancy box to anchor
        $("a#aOpenXL").fancybox({
            "onStart": function () { setPopSize(); },
            "onCleanup": function () { killZoomer(); },
            "overlayOpacity": 0.8,
            "overlayColor": "#000000",
            "autoDimensions": true,
            "titleShow": false,
            "transitionIn": 'fade',
            "transitionOut": 'none',
            "showCloseButton": true,
            'type': 'inline'
        });

	}




}

//**********************************************************************************
//** Set big image
function setHero(jQO) {
	var imgHero = $("[alt_id='imgLarge']");
	var srcHero = jQO.attr("alt_lgsrc");
	var dvCaption = $("[alt_id='dvCaption']");
	var dvFeature = $("#divFeatureXLarge")
	
	//special "IF" for photo-gallery page only.
	if (imgHero.length == 0) {
		imgHero = $("[alt_id='imgXLargePG']");
		srcHero = jQO.attr("alt_xlgsrc");
		
	}

	dvCaption.text("");
		
	//animate and set image
	imgHero.fadeOut("fast");
	imgHero.attr("src", "/images/icons/spacer.gif");
	imgHero.queue(function() {
		$(this).attr("src", srcHero);
		$(this).attr("alt_xlgsrc", jQO.attr("alt_xlgsrc"));
		$(this).attr("alt_xxlgsrc", jQO.attr("alt_xxlgsrc"));
		dvCaption.width(imgHero.width());
		$(this).dequeue();

	});

	//set larger view images	
	$("#imgXLarge").attr("src", jQO.attr("alt_xlgsrc"));
	$("#aZoom").attr("href", jQO.attr("alt_xxlgsrc"));

	//show new image
	imgHero.fadeIn("fast");
	
	//set caption
	dvCaption.text(jQO.attr("alt_caption"));
	

	//rebind anchor
	bindPopupToggle(jQO);
	

}

//**********************************************************************************
//** set popup images for first run.
function initPop() {
	var imgLarge = $("[alt_id='imgLarge']");

	//special "IF" for photo-gallery page only.
	if (imgLarge.length == 0) {
		imgLarge = $("[alt_id='imgXLargePG']");
	}
	
	//set larger view images
	$("#imgXLarge").attr("src", imgLarge.attr("alt_xlgsrc"));
	$("#aZoom").attr("href", imgLarge.attr("alt_xxlgsrc"));

}

//**********************************************************************************
//** set popup window size
function setPopSize() {
	var imgXL = new Image();
	imgXL.src = $("#imgXLarge").attr("src");

	$("#dvXLargeImage").width(imgXL.width + 255);
	$("#dvXLargeImage").height(imgXL.height + 5);
	//alert(imgXL.width);
}

//**********************************************************************************
//** initialize zoom feature
function initZoomer() {
	var jqZoom_options = {
		zoomWidth: 245,
		zoomHeight: 323,
		imageOpacity: 0.5, //use when zoomTYpe is reversed
		xOffset: 7,
		yOffset: 0,
		title: false,
		showPreload: false,
		alwaysOn: false,
		position: "right" //and MORE OPTIONS
	};

	$("#aZoom").jqzoom(jqZoom_options);

}

//**********************************************************************************
//** kill zoom feature
function killZoomer() {
	//get rid of zoomer stuff 
	$("#aZoom").unbind();
	$(".jqZoomWindow").remove();
	$(".jqZoomPup").remove();
	
	//reset other items
	$("#aZoom").click(function(e) { e.preventDefault(); });
	$('#enableZoom').attr('checked', false);
}

//**********************************************************************************
//** toggle zooming
function toggleZoom(jQO) {
	if (jQO.is(':checked')) {
		initZoomer();
	} else {
		killZoomer();
	}
}
