/***** SETTINGS *****/

var transSpeed = 250;
var isZoomSet = false;

var $img;
var $zoomIn;
var $zoomOut;

var player;

$(document).ready(function() {

    if ($.browser.msie && $.browser.version == '8.0') {
        (function() {
            var $tray = $('#tray'),
				$parent = $tray.parent(),
				$clone = $tray.clone(true);
            $tray.remove();
            $parent.append($clone);
        } ());
    }

    getObjects();

    setDraggable();

    setZoomers();

    // called by jw player when it's finished loading
    window.playerReady = function(thePlayer) {

        //alert(thePlayer.id);
        player = window.document[thePlayer.id];

        // unmute the player
        if (player) {
            player.sendEvent('MUTE', false);
        }

    };

    $('#contest_archive_scrollable_content').jScrollPane({
        dragMaxHeight: 14,
        animateTo: !($.browser.msie && parseInt($.browser.version) == 8),
        showArrows: true
    });

    $('#contest_text_scrollable_content').jScrollPane({
        dragMaxHeight: 14,
        animateTo: !($.browser.msie && parseInt($.browser.version) == 8),
        showArrows: true
    });

    $('#video_contest_submit').click(function() {
        $('#contests_layout_box_form').hide();
        $('#contests_layout_box_upload_message').show();
    });

    $('#contest_archive_window').css('height', '');
    $('#contest_archive_window').css('padding', '');
    $('#contest_archive_window').hide();

    var showingContestDropdown = 0;
    $('.contest_archive_container a').live('click', function() {
        if (showingContestDropdown) {
            $('#contest_archive_window').slideUp();
            showingContestDropdown = 0;
        }
        else {
            $('#contest_archive_window').slideDown();
            showingContestDropdown = 1;
        }
    });

    var showingPollDropdown = 0;
    $('.poll_archive_container a').click(function() {
        if (showingPollDropdown) {
            $('#contest_archive_window').slideUp();
            showingPollDropdown = 0;
        }
        else {
            $('#contest_archive_window').slideDown();
            showingPollDropdown = 1;
        }
    });

    // set functions for sliding contest filmstrip images
    $('#contests_weekly .select_left').click(function() {

        //check to see if we can slide
        if ($('#contests_weekly .filmstripbox a:first').position().left > 0) {
            return false;
        }

        $('#contests_weekly .filmstripbox a').animate({ 'left': '+=410' }, 400);
    });

    $('#contests_weekly .select_right').click(function() {

        //check to see if we can slide
        if ($('#contests_weekly .filmstripbox a:last').position().left < 420) {
            return false;
        }

        $('#contests_weekly .filmstripbox a').animate({ 'left': '-=410' }, 400);
    });

});

/*
 * intFilmPosition - The index of the selected film item
 * function advances the film strip based on the seleced film item 
 *
*/
function advanceFilmStrip(intFilmPosition) {

    var intFilmsPerFrame = 5;

    var intOffset = 410;

    var intAdvanceStrip = 0;

    var intAdvancePixels = 0;

    intAdvance = Math.floor( (intFilmPosition - 1) / intFilmsPerFrame);

    intAdvancePixels = intAdvance * intOffset;

    if (intAdvance > 0) {

        $('#contests_weekly .filmstripbox a').animate({ 'left': '-=' + intAdvancePixels }, 400);
        
    }   //end if

}   

function switchContestSplashImage(strFullPath) {

    var objElement = document.getElementById("contest_image");

    if (objElement != null) 
    {
        objElement.style.backgroundImage = "url(" + strFullPath + ")";
        
    }
}

function contests_soundOff()
{
    $('#player_sound_on').hide();
    $('#player_sound_off').show();

    if(player)
    {
        player.sendEvent('MUTE', true);
    }
}

function contests_soundOn()
{
    $('#player_sound_on').show();
    $('#player_sound_off').hide();
    
    if(player)
    {
        player.sendEvent('MUTE', false);
    }
}

function contests_replay()
{
    if(player) {
        player.sendEvent('SEEK', 0);
        player.sendEvent('PLAY', true);
    }
}

function contests_playerPlay() {

    if (player) {

        if (player.sendEvent)
            player.sendEvent('PLAY', true);
    }

}

function contests_playerPause()
{
    if(player)
    {
        if(player.sendEvent)
            player.sendEvent('PLAY', false);
    }
}

function contests_playerReplay()
{
    contests_replay();
}

function contests_setAutoPlay(autoPlayValue)
{
    setting.autoPlay = autoPlayValue;
}

function getObjects() {
    $img = $('#entry_preview .contest_entry_box #divContestEntryPreview.crop-image img#ctl00_ContentPlaceHolder1_imgContestEntryImage');
    $zoomIn = $('.contest_entry_box .zoom_image .zoom_in');
    $zoomOut = $('.contest_entry_box .zoom_image .zoom_out');
}

function setDraggable() {

    $img.draggable();

}

function setZoomers() {
    var pxStep = 50;

    $zoomIn.click(function() {
        $img.stop().animate({ height: '+=' + pxStep + 'px', left: '-=' + (pxStep / 2) + 'px', top: '-=' + (pxStep / 2) + 'px' }, transSpeed);
    });
    $zoomOut.click(function() {
        $img.stop().animate({ height: '-=' + pxStep + 'px', left: '+=' + (pxStep / 2) + 'px', top: '+=' + (pxStep / 2) + 'px' }, transSpeed);
    })

}

function changeParentHash(hashLink) {
    parent.window.location.hash = hashLink;
    
    return false;
    
}


//sharing code
function share(network, href) {

    var url = '', title;
    if (href == undefined || href == '') {
        href = parent.parent.document.location;
        // remove the # sound from the href tag
        var str = new String(href);
        str = str.replace('#', '')
        href = str;
    }

    if (network == 'facebook') {
        url = 'http://www.facebook.com/sharer.php?u=' + href;
    }
    else if (network == 'digg') {
        url = 'http://digg.com/submit?phase=2&url=' + href;
    }
    else if (network == 'del' || network == 'delicious') {
        url = 'http://del.icio.us/post?url=' + href;
    }
    else if (network == 'myspace') {
        url = 'http://www.myspace.com/index.cfm?fuseaction=postto&u=' + href;
    }
    else if (network == 'twitter') {
        url = 'http://twitter.com/share?text=Check out this link: &url=' + href;
    }
    else if (network == 'su' || network == 'stumble') {
        url = 'http://www.stumbleupon.com/submit?url=' + href;
    }
    else if (network == 'email') {
        url = "mailto:?subject=We're a Lot Like You: PEMCO Insurance&body=" + href;
    }
    else if (network == 'bookmark') {
        title = document.title;

        if ($.browser.msie && document.all) {
            window.external.AddFavorite(href, title);
        }

        else if ($.browser.mozilla && window.sidebar) {
            window.sidebar.addPanel(title, href, "");
        }

        else {
            pemco.ajax.flash('Your browser does not support automatic bookmarking. Please use your browsers bookmarking features to save this page.', 'Notice', 10000);
        }

        return false;
    }

    if (url.length) {
        if (network == 'email') {
            window.location = url;
        }
        else {
            window.open(url);
        }
    }

    return false;
}

// -------------------
//enable sharing links
$('.facebook')
	.live('click', function() {
	    share('facebook');
	    return false;
	})
	.live('mouseover', function() {
	    $(this).attr('title', 'Share on Facebook').attr('rel', 'sidebar');
	})

$('.digg')
	.live('click', function() {
	    share('digg');
	    return false;
	})
	.live('mouseover', function() {
	    $(this).attr('title', 'Digg it!').attr('rel', 'sidebar');
	})

$('.del')
	.live('click', function() {
	    share('delicious');
	    return false;
	})
	.live('mouseover', function() {
	    $(this).attr('title', 'Bookmark on delicious').attr('rel', 'sidebar');
	})

$('.myspace')
	.live('click', function() {
	    share('myspace');
	    return false;
	})
	.live('mouseover', function() {
	    $(this).attr('title', 'Share on MySpace').attr('rel', 'sidebar');
	})

$('.twitter')
	.live('click', function() {
	    share('twitter');
	    return false;
	})
	.live('mouseover', function() {
	    $(this).attr('title', 'Share on Twitter').attr('rel', 'sidebar');
	})

$('.su')
	.live('click', function() {
	    share('su');
	    return false;
	})
	.live('mouseover', function() {
	    $(this).attr('title', 'Share on StumbleUpon').attr('rel', 'sidebar');
	})

$('.email')
	.live('click', function() {
	    share('email');
	    return false;
	})
	.live('mouseover', function() {
	    $(this).attr('title', 'Email this page to a friend').attr('rel', 'sidebar');
	})

$('.bookmark')
	.live('click', function() {
	    share('bookmark');
	    return false;
	})
	.live('mouseover', function() {
	    $(this).attr('title', 'Bookmark this page').attr('rel', 'sidebar');
	})

