/* Author: agreig

*/

var JQphotoComp = (function() {

    var page = {

        init: function() {
            this.cufon();
        },

        cufon: function() {

            // Set cufon fonts
            //Medium
            Cufon.replace('#header a.enter, #header .like-above, #header .prizes .fReplaceMed, #main .banner h3, #main .banner h4', {
                fontFamily: 'ITC Avant Garde Gothic',
                letterSpacing: '-1px',
                fontWeight: 500
            });
            //Demi
            Cufon.replace('#header .content h2, #main .total-search fieldset input#btnSearch', {
                fontFamily: 'ITC Avant Garde Gothic',
                letterSpacing: '-1px',
                fontWeight: 600,
                hover: true
            });
            //Demi - no letter spacing (for smaller fonts)
            Cufon.replace('#header .prizes ul li .count, #main .compbox .comp-fields label, .fReplaceDemi', {
                fontFamily: 'ITC Avant Garde Gothic',
                letterSpacing: '0px',
                fontWeight: 600,
                hover: true
            });
            //Bold 
            Cufon.replace('#header .prize-star, #header .prizes ul li .status, #header .prizes ul li p, .prizes .fReplaceMed strong, #header a.enter strong, #header .like-above strong,  #main .banner h3 strong', {
                fontFamily: 'ITC Avant Garde Gothic',
                letterSpacing: '-1px',
                fontWeight: 700
            });
        }
    };

    var gallery = {

        init: function() {
            this.ajax();
        },

        ajax: function() {

            $('#searchResults .showMore').live('click', function(e) {
                if (e.preventDefault)
                    e.preventDefault();

                doSearch($(this).attr('href'), null, function(response) {
                    $('#searchResults ul').append(response);
                    Cufon.refresh();
                    gallery.layout();
                });

                return false;
            });

            $('#txtSearch').unbind('keydown').bind('keydown', function(e) {
                if (e.which == 13) {
                    if (e.preventDefault)
                        e.preventDefault();

                    var keyword = $('#txtSearch').val();
                    searchPhoto(keyword, function(response) {
                        $('#searchResults ul').hide().empty().html(response).fadeIn('slow');
                        Cufon.refresh();
                        gallery.layout();
                    });

                    _gaq.push(['_trackEvent', 'starjump', gaAction, '2_findjump']);

                    return false;
                }
            });

            $('#btnSearch').unbind('click').bind('click', function(e) {
                if (e.preventDefault)
                    e.preventDefault();

                var keyword = $('#txtSearch').val();
                searchPhoto(keyword, function(response) {
                    $('#searchResults ul').hide().empty().html(response).fadeIn('slow');
                    Cufon.refresh();
                    gallery.layout();
                });

                _gaq.push(['_trackEvent', 'starjump', gaAction, '2_findjump']);

                return false;
            });

            searchPhoto('', function(response) {
                $('#searchResults ul').hide().empty().html(response).fadeIn('slow');
                Cufon.refresh();
                gallery.layout();
            });

            function searchPhoto(keyword, callback) {
                var data = {
                    'keyword': keyword
                };
                doSearch('/facebook/ajax/searchphoto.aspx', data, callback);
            }

            var currentSearchAjax;
            function doSearch(url, data, callback) {
                if (currentSearchAjax && currentSearchAjax.abort)
                    currentSearchAjax.abort();

                currentSearchAjax = $.ajax({
                    url: url,
                    data: data,
                    cache: false,
                    success: function(response, textStatus, jqXHR) {
                        if ($.isFunction(callback)) {
                            callback(response);
                        }
                    }
                });
            }
        },
        photoZoom: function(elem) {

            var $info = $(elem).find('.jump-info'),
					url = $info.find('a').attr('href'),
					name = $info.find('h3').html(),
					location = $info.find('h4').html(),
					caption = $info.find('p').html(),
					pageWidth = $('#container').width();

            var photoBox = $('<div id="photoZoom"><a class="overlayClose ir" href="#">Close</a><img src="' + url + '" alt="Star Jump entry" /><div class="photoCaption">' + '<h2>' + name + ' <strong>' + location + '</strong></h2><p>' + caption + '</p></div></div>"');

            photoBox.click(function(e) { e.stopPropagation(); }).find('a').click(function(e) { e.preventDefault(); photoBox.parent().css('z-index', ''); photoBox.remove(); });
            photoBox.find('img').load(function() {
                var $me = $(this),
				horzDiff = pageWidth - $me.width(),
				offset = photoBox.offset();

                //photoBox.css({'position':'fixed', 'left':- horzDiff/2, 'top':offset.top}).fadeIn();
                photoBox.fadeIn();
            });
            $(elem).click(function(e) { e.stopPropagation(); });
            $('body').click(function(e) { photoBox.remove(); $('body').unbind('click'); });

            photoBox.hide().insertAfter($info).parent().css('z-index', '9999');

            _gaq.push(['_trackEvent', 'starjump', gaAction, '3_jumpphoto']);
        },

        layout: function() {

            var lists = $('#searchResults').find('li.item');
            classes = ['one', 'two', 'three', 'four'],
				i = 0;

            $.each(lists, function() {
                var $this = $(this);

                $this.removeClass('one two three four');
                $this.addClass(classes[i]);
                $this.find('.jump a').click(function(e) {
                    e.preventDefault();
                    if ($('#photoZoom').length) {
                        $('#photoZoom').parent().css('z-index', '');
                        $('#photoZoom').remove();
                    }
                    gallery.photoZoom($this);
                });
                $this.find('.jump-info').click(function(e) {
                    e.preventDefault();
                    if ($('#photoZoom').length) {
                        $('#photoZoom').parent().css('z-index', '');
                        $('#photoZoom').remove();
                    }
                    gallery.photoZoom($this);
                });

                if (i == 3) {
                    i = 0;
                } else {
                    i++;
                }
            });
        }
    };

    var uploadForm = {
        init: function() {
            //GA specific event handlers.
            $('#txtFirstName').die('focus').live('focus', function(e) {
                _gaq.push(['_trackEvent', 'starjump', gaAction, '1_firstname']);
            });
            $('#txtLastName').die('focus').live('focus', function(e) {
                _gaq.push(['_trackEvent', 'starjump', gaAction, '2_lastname']);
            });
            $('#txtEmail').die('focus').live('focus', function(e) {
                _gaq.push(['_trackEvent', 'starjump', gaAction, '3_email']);
            });
            $('#ddlYear').die('focus').live('focus', function(e) {
                _gaq.push(['_trackEvent', 'starjump', gaAction, '4_dob']);
            });
            $('#txtPhotoTitle').die('focus').live('focus', function(e) {
                _gaq.push(['_trackEvent', 'starjump', gaAction, '6_phototitle']);
            });
            $('#ddlPhotoLocation').die('focus').live('focus', function(e) {
                _gaq.push(['_trackEvent', 'starjump', gaAction, '7_photolocation']);
            });
            $('#chkTerms').die('change').live('change', function(e) {
                if ($(this).is(":checked")) {
                    _gaq.push(['_trackEvent', 'starjump', gaAction, '8a_termstick']);
                }
                else {
                    _gaq.push(['_trackEvent', 'starjump', gaAction, '8b_termsuntick']);
                }
            });
            $('#chkPermission').die('change').live('change', function(e) {
                if ($(this).is(":checked")) {
                    _gaq.push(['_trackEvent', 'starjump', gaAction, '9a_photorightstick']);
                }
                else {
                    _gaq.push(['_trackEvent', 'starjump', gaAction, '9b_photorightsuntick']);
                }
            });
            $('#chkNotEntered').die('change').live('change', function(e) {
                if ($(this).is(":checked")) {
                    _gaq.push(['_trackEvent', 'starjump', gaAction, '10a_photoprevioustick']);
                }
                else {
                    _gaq.push(['_trackEvent', 'starjump', gaAction, '10b_photopreviousuntick']);
                }
            });
            $('#chkAge').die('change').live('change', function(e) {
                if ($(this).is(":checked")) {
                    _gaq.push(['_trackEvent', 'starjump', gaAction, '11a_agetick']);
                }
                else {
                    _gaq.push(['_trackEvent', 'starjump', gaAction, '11b_ageuntick']);
                }
            });
        }
    };

    return {

        init: function() {
            page.init();
            gallery.init();
            uploadForm.init();

            $('input[placeholder]').placeholder();
        }

    };

})();

/**
 * Run this stuff
 *
 */
$(document).ready(function() {
	JQphotoComp.init();
}); 

