﻿/*================================
 Duck Brand 
 
 Last Updated By: Ryan Perry
 Last Updated: 11/14/2011
================================*/
var currentModal;
var allImagesLoaded;
var pendingImageURL = null;


jQuery(window).load(
    function () {
        allImagesLoaded = true;
		
		jQuery('.gallery-modal div.image img.modal-view').hide();
		
        //All images loaded, load in any pending image on the modal.
        if (pendingImageURL != null)
            jQuery('.gallery-modal div.image img.modal-view').attr("src", pendingImageURL);
    }
);

/*================================================
 Gallery javascript
================================================*/
function showNextGalleryImage() {
    var currentIndex = jQuery(currentModal).parent().parent().attr("ImageIndex");
    var nextIndex = parseInt(currentIndex) + 1;
    var nextImage = jQuery('.gallery-item div.image[ImageIndex="' + nextIndex + '"] a img');

    //wrap to first image
    if (nextImage.length == 0)
        nextImage = jQuery('.gallery-item div.image[ImageIndex="0"] a img');

    if (nextImage.length > 0)
        showModalGalleryImage(nextImage[0]);
}

function getMaxIndexOfGalleryImages() {
    var maxIndex = 0;
    jQuery('.gallery-item div.image').each(function () {
        var image = jQuery(this);
        var index = image.attr('ImageIndex');
        if (parseInt(index) > maxIndex)
            maxIndex = parseInt(index);
    });

    return maxIndex;
}

function showPreviousGalleryImage() {
    
    var currentIndex = jQuery(currentModal).parent().parent().attr("ImageIndex");
    var prevIndex = parseInt(currentIndex) - 1;

    //wrap to last image
    if (prevIndex < 0) {
        var maxIndex = getMaxIndexOfGalleryImages();
        prevIndex = maxIndex;
    }

    var prevImage = jQuery('.gallery-item div.image[ImageIndex="' + prevIndex + '"] a img');

    if (prevImage.length > 0)
        showModalGalleryImage(prevImage[0]);
}


/*================================================
 Detail Modal
================================================*/
function showNextModalImage() {
    var currentIndex = jQuery(currentModal).attr("ImageIndex");
    var nextIndex = parseInt(currentIndex) + 1;
    var nextImage = jQuery('#promotion-content div.preview .image img[ImageIndex="' + nextIndex + '"]');

    //wrap to first image
    if (nextImage.length == 0)
        nextImage = jQuery('#promotion-content div.preview .image img[ImageIndex="0"]');

    if (nextImage.length > 0)
        showModalImage(nextImage[0]);
}

function getMaxIndexOfModalImages() {
    var maxIndex = 0;
    jQuery('#promotion-content div.preview .image img').each(function () {
        var image = jQuery(this);
        var index = image.attr('ImageIndex');
        if (parseInt(index) > maxIndex)
            maxIndex = parseInt(index);
    });

    return maxIndex;
}

function showPreviousModalImage() {
    
    var currentIndex = jQuery(currentModal).attr("ImageIndex");
    var prevIndex = parseInt(currentIndex) - 1;

    //wrap to last image
    if (prevIndex < 0) {
        var maxIndex = getMaxIndexOfModalImages();
        prevIndex = maxIndex;
    }

    var prevImage = jQuery('#promotion-content div.preview .image img[ImageIndex="' + prevIndex + '"]');

    if (prevImage.length > 0)
        showModalImage(prevImage[0]);
}

function showModalImage(img) {
    currentModal = img;

    //If not all images loaded, save pending image url and load progress instead
    if (!allImagesLoaded) {
		//jQuery('.gallery-modal div.image img').attr('width', '66').attr('height', '66');
        pendingImageURL = jQuery(img).attr("src").split("?")[0];
    }

    //jQuery('.gallery-modal div.image img').attr("src", "/images/promotions/stick-or-treat/2011/sot-loader.gif");
    //jQuery('.gallery-modal div.shell a.view-profile').attr("href", jQuery(img).parent().parent().attr("profile"));
    //jQuery('.gallery-modal div.shell h3').text(jQuery(img).parent().parent().attr("title"));
    jQuery('body').append(jQuery('.gallery-modal'));
	jQuery('.gallery-modal').jqm().jqmShow();

    if (allImagesLoaded) {
		jQuery('.gallery-modal div.image img.loading').hide();
		jQuery('.gallery-modal div.image img.modal-view').show();
		
        jQuery('.gallery-modal div.image img.modal-view').attr("src", jQuery(img).attr("src").split("?")[0]);
    }
}




/*================================================
 Top 4 entries javascript
================================================*/
function showNextEntryImage() {
    var currentIndex = jQuery(currentModal).parent().parent().attr("ImageIndex");
    var nextIndex = parseInt(currentIndex) + 1;
    var nextImage = jQuery('.recent-entry div.image[ImageIndex="' + nextIndex + '"] a img');

    //wrap to first image
    if (nextImage.length == 0)
        nextImage = jQuery('.recent-entry div.image[ImageIndex="0"] a img');

    if (nextImage.length > 0)
        showModalGalleryImage(nextImage[0]);
}

function getMaxIndexOfEntryImages() {
    var maxIndex = 0;
    jQuery('.recent-entry div.image').each(function () {
        var image = jQuery(this);
        var index = image.attr('ImageIndex');
        if (parseInt(index) > maxIndex)
            maxIndex = parseInt(index);
    });

    return maxIndex;
}

function showPreviousEntryImage() {

    var currentIndex = jQuery(currentModal).parent().parent().attr("ImageIndex");
    var prevIndex = parseInt(currentIndex) - 1;

    //wrap to last image
    if (prevIndex < 0) {
        var maxIndex = getMaxIndexOfEntryImages();
        prevIndex = maxIndex;
    }

    var prevImage = jQuery('.recent-entry div.image[ImageIndex="' + prevIndex + '"] a img');

    if (prevImage.length > 0)
        showModalGalleryImage(prevImage[0]);
}

function showModalGalleryImage(img) {
    currentModal = img;

    //If not all images loaded, save pending image url and load progress instead
    if (!allImagesLoaded) {
        pendingImageURL = jQuery(img).attr("src").split("?")[0];
    }

    jQuery('.gallery-modal div.image img').attr("src", "/images/ducktivity-loader.gif");
    jQuery('.gallery-modal div.shell a.view-profile').attr("href", jQuery(img).parent().parent().attr("profile"));
    jQuery('.gallery-modal div.shell h3').text(jQuery(img).parent().parent().attr("title"));
    jQuery('body').append(jQuery('.gallery-modal'));
	jQuery('.gallery-modal').jqm().jqmShow();

    if (allImagesLoaded) {
        jQuery('.gallery-modal div.image img').attr("src", jQuery(img).attr("src").split("?")[0]);
    }
}



jQuery(document).ready(function ($) {

    /*================================================
    Cufon Font Replacement
    ================================================*/    
    Cufon.replace('#promotion-content.home .promotion-secondary .callout h3', { textShadow: '2px 2px 3px #000000' });
    Cufon.replace('#promotion-content.home .promotion-main h2', { textShadow: '3px 3px 4px #000000' });
    Cufon.replace('#promotion-content.home .winners h3', { textShadow: '2px 2px 3px #000000' });
	
	Cufon.replace('#promotion-content.winners h2', { textShadow: '3px 3px 4px #000000' });
	


    /*================================================
    Preview Image Cycle
    ================================================*/
    $('#promotion-content div.preview .image').after('<ul id="preview-nav">').cycle({
        fx: 'none',
        timeout: 0,
        pager: '#preview-nav',
        animIn: {
            top: 12,
            left: 11
        },
        pagerAnchorBuilder: function (idx, slide) {
            return '<li><a href="#"><img src="' + slide.src + '" width="46" height="46" /></a></li>';
        }
    });


    /*================================================
    Add First/Last Class
    ================================================*/
    /*---- UL/OL List Items ----*/
    $('ul li:first-child, ol li:first-child').addClass('first');
    $('ul li:last-child, ol li:last-child').addClass('last');

    /*---- DL List Items ----*/
    $('dl dt:first-child, dl dd:first-child').addClass('first');
    $('dl dt:last, dl dd:last-child').addClass('last');

    /*---- Callouts ----*/
    $('#promotion-content .callout:first-child').addClass('first');
    $('#promotion-content .callout:last-child').addClass('last');

    /*---- Sections ----*/
    $('#promotion-content .section:first-child').addClass('first');
    $('#promotion-content .section:last-child').addClass('last');


    /*================================================
    Clear Gallery Listing Row Margins
    ================================================*/
    $('#promotion-content div.gallery .gallery-item:nth-child(4n)').css({ 'margin-right': '0px' });


    /*================================================
    Profile Modal
    ================================================*/
    $('#promotion-content div.preview .image img').each(function (idx) {

        $(this).attr("ImageIndex", idx);

    });

    $('.modal-trigger').each(function () {
        $(this).bind('click', function () {
            showModalImage(this)
        });
    });




    /*================================================
    Entry Form
    ================================================*/
    /*---- File Input Replacement ----*/
    if ($.browser.msie && $.browser.version == "6.0") {
        $("#promotion-content li.browse input[type=file]").filestyle({
            image: "/images/promotions/stick-or-treat/ie6/btn-browse.gif",
            imageheight: 33,
            imagewidth: 73,
            width: 250
        });
    } else {

        $("#promotion-content .browse input[type=file]").filestyle({
            image: "/images/promotions/stick-or-treat/btn-browse.png",
            imageheight: 33,
            imagewidth: 73,
            width: 250
        });

    }

    /*---- Phone AutoTab ----*/
    $('input.input-text.small').keyup(function () {
        if ($(this).val().length === $(this).attr('maxlength')) {
            //var id = $(this).attr('id').match(/^autofocus(\d+)$/[1];
            //var nextId = Number(id) + 1;
            //$('#autofocus' + nextId).focus()
            $(this).next('input.input-text.small').focus();
        }
    });

    $('#btnSubmit').click(function () {
        $(this).hide().next().show();
    });

    $('input.input-text, textarea.input-text').clearDefault();



    /*================================================
    Image Cropping
    ================================================*/
    $('.image-cropping').each(function (unusedIndex, container) {
        container = $(container);

        var image = container.find("img.crop-image");

        var update = function (c) {
            if (parseInt(c.w) <= 0 || parseInt(c.h) <= 0) return;
            container.find('.img_X').val(c.x);
            container.find('.img_Y').val(c.y);
            container.find('.img_W').val(c.w);
            container.find('.img_H').val(c.h);
        }

        image.Jcrop({
            onChange: update,
            onSelect: update,
            bgColor: 'black',
            bgOpacity: 0.6,
            aspectRatio: 1,
            minSize: [130, 130],
            maxSize: [500, 500],
            boxWidth: 700,
            setSelect: [0, 0, 130, 130]
        });
    });


    /*================================================
    Hover Fix
    ================================================*/
    /*---- Button Hover ----*/
    $('input[type=button], input[type=submit]').hover(
	function () { $(this).addClass("hover"); },
	function () {
	    $(this).removeClass("hover");
	});

    $('#promotion-content .browse div').hover(
	function () { $(this).addClass("hover"); },
	function () {
	    $(this).removeClass("hover");
	});


    /*================================================
    Open External Links in a new window
    ================================================*/
    $('.new-window').attr('target', '_blank');


/*================================================
 Open External Links in a new window
================================================*/
$('#promotion-content.gallery .pagination div.pagination-links a[disabled="disabled"]').addClass('active');


    /*================================================
    Video Player
    ================================================*/
    if ($().VideoJS) {
        $('video').VideoJS()
    }

    /*================================================
    IE Layout Bugs
    ================================================*/
    if ($.browser.msie && $.browser.version == "8.0") {

    }


    /*================================================
    IE6 PNG Fix
    ================================================*/
    if ($.browser.msie && $.browser.version == "6.0") {
        $('#promotion-nav').ifixpng();
        $('#promotion-content.begin .section div.image ').ifixpng();
        $('#promotion-content.decorating .section div.image').ifixpng();
        $('#main.stick-or-treat #promotion-entries .recent-entry .image').ifixpng();
        $('#promotion-content.home .winners .image').ifixpng();
    }

    if ($.browser.msie) {
        $('#promotion-content.home .promotion-secondary .callout p').css({ 'letter-spacing': '-1px' });
    }


});
