var CommentsSectionId = "CommentsSection";
var CommentTextBoxId = "txtComment";
var CommentSaveSuccessfulText = "Comment save succesful.  Your comment will show up in a few minutes.";
var CommentSaveFailureText = "Error saving comment.";
var requiredMajorVersion = 9;
var requiredMinorVersion = 0;
var requiredRevision = 0;

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

function setGlobalClickEvents()
{
    $('a#discussion-tab').click(function(event){
        $(this).removeClass('discussion');
        $(this).addClass('discussion on');
        $('a#details-tab').removeClass('details on');
        $('a#details-tab').addClass('details');
        $('#details-content').hide();            
        $('#discussions-content').show();
        event.preventDefault();
    });
    $('a#details-tab').click(function(event){
        $(this).removeClass('details');
        $(this).addClass('details on');
        $('a#discussion-tab').removeClass('discussion on');
        $('a#discussion-tab').addClass('discussion');
        $('#details-content').show();
        $('#discussions-content').hide();
        event.preventDefault();
    });
    $('#cancelComment').click(function(event){
        HideAddComment();
        event.preventDefault();
    });
    $('#thankYouClose').click(function(event){
        HideAddComment();
        event.preventDefault();
    });
}


function setGalleryClickEvents()
{
    setGlobalClickEvents();
}

function setProfileClickEvents()
{
    $(".messages-and-comments .tabbedNav a").click(function(event){
        $('.messages-and-comments .tabbedNav li').removeClass('current');
        $(this).parent().addClass('current');
        var index = $(this).attr('id');
        var selectedDiv = ".messages-and-comments .messages-bg div:nth-child(" + index + ")";
        $('.messages-and-comments .messages-bg > div').hide();
        $(selectedDiv).show();
        event.preventDefault();
    });
    
    $(".progressionInfo .tabbedNav a").click(function(event){
        if ($(this).parent().attr('class') != 'current')
        {
            $('.progressionInfo .tabbedNav li').removeClass('current');
            $(this).parent().addClass('current');
            $('.progressionInfo > div').toggle();
            $('.progressionInfo > p').toggle();
        }
        event.preventDefault();
    });
    
    $('#profileNav a').click(function(event){
        $('#profileNav a').removeClass();
        $('#profileBlock .featuredVideos').hide();
        $('#profileBlock .featuredGraphics').hide();
        $('#containerDetails').hide();
        $('#containerProfile').show();
        $('.view-details a').unbind("click");
        
        switch($(this).attr('id'))
        {
            case "1":
            {
                $('#profileNav').removeClass();
                $('#profileNav').addClass('profileNav glow-first');
                $('#profileBlock div:nth-child(2)').show();
                event.preventDefault();
                break;
            }
            case "2":
            {
                $('#profileNav').removeClass();
                $('#profileNav').addClass('profileNav glow-middle');
                $('#profileBlock div:nth-child(3)').show();
                event.preventDefault();
                break;
            }
            case "3":
            {
                $('#profileNav').removeClass();
                $('#profileNav').addClass('profileNav glow-last');
                $('#profileBlock div:nth-child(4)').show();
                break;
            }
        }
        
        $(this).addClass('active');        
    });    

    $('.friend-requests a').click(function(event)
    {
        $('.friendships').toggle();
        $('#FriendListButton').toggle();
        $('#FriendRequestButton').toggle();
        event.preventDefault();
    }
    );
    
    
    $('.friend-list a').click(function(event)
    {
        $('.friendships').toggle();
        $('#FriendRequestButton').toggle();
        $('#FriendListButton').toggle();
        event.preventDefault();
    }
    );
    
    
    $('.back a').click(function(event){
        $('.container').toggle();
        event.preventDefault();
    });
    
    setGlobalClickEvents();
}

function setNavigation(page)
{
    switch(page)
    {
        case "videogallery":
        {
            $('#skateReel_nav').addClass('on');
            $('#videos_nav').addClass('on');
            $('#skateReelPageHeaderImage').addClass('hdrVideos');
            break;
        }
        case "photogallery":
        {
            $('#skateReel_nav').addClass('on');
            $('#photos_nav').addClass('on');
            $('#skateReelPageHeaderImage').addClass('hdrPhotos');
            break;
        }
        case "profile":
        {
            $('#profile_nav').addClass('on');            
            $('#skateReelPageHeaderImage').addClass('hdrProfile');
            break;
        }
        case "yourprofile":
        {
            $('#profile_nav').addClass('on');
            $('#skateReelPageHeaderImage').addClass('hdrYourProfile');
            break;
        }
        case "faq":
        {
            $('#skateReel_nav').addClass('on');
            $('#faq_nav').addClass('on');
            $('#skateReelPageHeaderImage').addClass('hdrFaq');
            break;
        }
        default:
        {
            $('#skateReel_nav').addClass('on');
            $('#skateReelPageHeaderImage').addClass('hdrVideos');
            $('#skateReelPageHeaderImage').addClass('hdrDefault');
            break;
        }
    }
}

function setPageDefaults(page)
{
    var callDefaultMethod = true;
    switch(page)
    {
        case "videogallery":
        {
            setNavigation(page);            
            setGalleryClickEvents();
            break;
        }
        case "photogallery":
        {
            setNavigation(page);            
            setGalleryClickEvents();
            break;
        }
        case "faq":
        {
            setNavigation(page);
            callDefaultMethod = false;
            break;
        }
        case "profile":
        case "yourprofile":
        {
            setNavigation(page);
            setProfileClickEvents();
            break;
        }
        default:
        {
            setNavigation(page);
            callDefaultMethod = false;
            break;
        }
    }
    var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
    
    if (hasReqestedVersion == true)
    {
        if (callDefaultMethod == true)
        {
            defaultVideo();
        }
    }
    else
    {
        $('#video').html('<div id="noflash">You do not have the latest version of Flash, you can download it <a href="http://www.adobe.com/go/getflash/" target="_blank">here</a>.</div>');
    }
}

function setProfileNav(index)
{
    $('#profileNav a').removeClass();
    $('#profileBlock .featuredVideos').hide();
    $('#profileBlock .featuredGraphics').hide();
    
    switch(index)
    {
        case 1:
        {
            $('#profileNav').removeClass();
            $('#profileNav').addClass('profileNav glow-first');
            $('#profileBlock div:nth-child(2)').show();
            $('.movie a').addClass('active')
            break;
        }
        case 2:
        {
            $('#profileNav').removeClass();
            $('#profileNav').addClass('profileNav glow-middle');
            $('#profileBlock div:nth-child(3)').show();
            $('.picture a').addClass('active')
            break;
        }
    }
}

/*----------------------------------------------------------------------------------------------*/
/*----------------------------------Web Service Calls-------------------------------------------*/
/*----------------------------------------------------------------------------------------------*/

function callWS()
{
    SkateReel2.Web.WebServices.SkateReelService.HelloWorld(onSuccess, onFail);
}

function UpdateVideoContent(postId, videoPath, width, height, videoContainerId, swfFile, thumbUrl, scale, wmode, version, backgroundColor, fileId, platformId, personaName, personaId, rating, threadId)
{
    PerformPageCleanup();
    SkateReel2.Web.WebServices.SkateReelService.GetPostComments(personaName, platformId, fileId, ProcessCommentList, onFail);
    ToggleMetaDataDetailsOn();
    //-- get the assets details
    var params = new Array();
    params[0] = postId;
    params[1] = personaId;
    params[2] = fileId;
    params[3] = platformId;
    params[4] = personaName;
    params[5] = threadId;

    SkateReel2.Web.WebServices.SkateReelService.GetAssetDetails(postId, platformId, personaId, 1, fileId, RenderAssetDetails, onFail, params);
    WriteVideo(videoPath, width, height, videoContainerId, swfFile, thumbUrl, scale, wmode, version, backgroundColor, postId, fileId, platformId, personaId);
    UpdateDownloadCount(platformId, fileId);
    UpdateBrowserTitle(personaName, 'Video');
}

function UpdateImageContent(postId, imagePath, width, height, containerId, fileId, platformId, personaName, personaId, rating, threadId)
{
    PerformPageCleanup();
    SkateReel2.Web.WebServices.SkateReelService.GetPostComments(personaName, platformId, fileId, ProcessCommentList, onFail);
    ToggleMetaDataDetailsOn();
    //-- get the assets details
    var params = new Array();
    params[0] = postId;
    params[1] = personaId;
    params[2] = fileId;
    params[3] = platformId;
    params[4] = personaName;
    params[5] = threadId;
    SkateReel2.Web.WebServices.SkateReelService.GetAssetDetails(postId, platformId, personaId, 1, fileId, RenderAssetDetails, onFail, params);
    WriteImage(imagePath, width, height, containerId, postId, fileId, platformId, personaId);    
    UpdateDownloadCount(platformId, fileId);
    UpdateBrowserTitle(personaName, 'Photo');
}

function UpdateDownloadCount(platformId, fileId)
{
    SkateReel2.Web.WebServices.SkateReelService.UpdateDownloadCount(platformId, fileId, onSuccess_donothing, onFail);
}

function SkateReel2RateMedia(platformId, fileId, reviewerUserId, rating, needTotalVoteCount)
{
    SkateReel2.Web.WebServices.SkateReelService.UpdateRating(platformId, fileId, reviewerUserId, rating, needTotalVoteCount, onSuccess, onFail);
}

function AddPostComment(postId)
{
    var txtAddComment = $get(CommentTextBoxId);
    SkateReel2.Web.WebServices.SkateReelService.AddPostComment(postId, txtAddComment.value, onSuccess, onFail);
}

function ShowProfileVideoAsset(postId, videoPath, width, height, videoContainerId, swfFile, thumbUrl, scale, wmode, version, backgroundColor, fileId, platformId, personaName, personaId, rating, threadId)
{
    //show profile video asset
    WriteVideo(videoPath, width, height, videoContainerId, swfFile, thumbUrl, scale, wmode, version, backgroundColor, postId, fileId, platformId, personaId);
    UpdateDownloadCount(platformId, fileId);
    UpdateBrowserTitle(personaName, 'Video');
    $('.view-details').show();
    $('.view-details a').unbind("click").click(function(event){ShowProfileVideoDetails(postId, videoPath, width, height, videoContainerId, swfFile, thumbUrl, scale, wmode, version, backgroundColor, fileId, platformId, personaName, personaId, rating, threadId);event.preventDefault();});
}

function ShowProfilePhotoAsset(postId, imagePath, width, height, containerId, fileId, platformId, personaName, personaId, rating, threadId)
{
    //show profile photo asset
    WriteImage(imagePath, width, height, containerId, postId, fileId, platformId, personaId);    
    UpdateDownloadCount(platformId, fileId);
    UpdateBrowserTitle(personaName, 'Photo');
    $('.view-details').show();
    $('.view-details a').unbind("click").click(function(event)
    {
        ShowProfilePhotoDetails(postId, imagePath, width, height, containerId, fileId, platformId, personaName, personaId, rating, threadId);event.preventDefault();
    });
}

function ShowProfileVideoDetails(postId, videoPath, width, height, videoContainerId, swfFile, thumbUrl, scale, wmode, version, backgroundColor, fileId, platformId, personaName, personaId, rating, threadId)
{
    $('.container').toggle();
    SkateReel2.Web.WebServices.SkateReelService.GetPostComments(personaName, platformId, fileId, ProcessCommentList, onFail);
    ToggleMetaDataDetailsOn();
    //-- get the assets details
    var params = new Array();
    params[0] = postId;
    params[1] = personaId;
    params[2] = fileId;
    params[3] = platformId;
    params[4] = personaName;
    params[5] = threadId;
    
    SkateReel2.Web.WebServices.SkateReelService.GetAssetDetails(postId, platformId, personaId, 1, fileId, RenderAssetDetails, onFail, params);        
}

function ShowProfilePhotoDetails(postId, imagePath, width, height, containerId, fileId, platformId, personaName, personaId, rating, threadId)
{
    $('.container').toggle();
    SkateReel2.Web.WebServices.SkateReelService.GetPostComments(personaName, platformId, fileId, ProcessCommentList, onFail);
    ToggleMetaDataDetailsOn();
    //-- get the assets details
    var params = new Array();
    params[0] = postId;
    params[1] = personaId;
    params[2] = fileId;
    params[3] = platformId;
    params[4] = personaName;
    params[5] = threadId;

    SkateReel2.Web.WebServices.SkateReelService.GetAssetDetails(postId, platformId, personaId, 2, fileId, RenderAssetDetails, onFail, params);
}


function UserSearch()
{
    var platformId = $('#searchby').attr("selectedIndex") + 1;
    var url = "/Themes/SkateReel2/User/UserSearch.aspx?PersonaName=" + $('#keywords:text').val() + '&PlatformId=' +   platformId;
    openModal(url, 480, 400);
}

function SearchTags(keyword)
{
    //debugger;
}


/*----------------------------------------------------------------------------------------------*/
/*------------------------------------Helper Functions------------------------------------------*/
/*----------------------------------------------------------------------------------------------*/

function PerformPageCleanup()
{
    HideAddComment();
}

function ToggleMetaDataDetailsOn()
{
    $('#featuredMedia').hide();
    $('#mediaDetails').show();
    $('a#details-tab').click();
}

function ToggleMetaDataDetailsOff()
{
    $('#featuredMedia').show();
    $('#mediaDetails').hide();
}

//-- This may not be needed.
function Render(result, containerName)
{
    $(containerName).html(result).show();
    var displayElement = $get(containerName);
    displayElement.innerHTML = result;
    displayElement.style.visibility = "visible";
}

function ProcessCommentList(result)
{
    $('#CommentsSection').html(result);
}

function RenderAssetDetails( result, params)
{
    $('#DetailsSection').html(result);
}

function ShowAddComment(postId)
{
    $('#postComment').hide();
    $('#commentsContainer').removeClass('pane discussion discussionComments');
    $('#commentsContainer').addClass('pane discussion discussionPost');
    $('#writeComment').show();
    $('#writeCommentButton').unbind("click").click(function(event){AddPostComment(postId);event.preventDefault();});
    $('#profileNav').hide();
}

function HideAddComment()
{
    $('#writeComment').hide();
    $('#thankYouMessageContainer').hide();
    $('#commentsContainer').removeClass('pane discussion discussionPost');
    $('#commentsContainer').addClass('pane discussion discussionComments');
    $('#postComment').show();
}

function ShowAddCommentThanks(result)
{
    $('#writeComment').hide();
    $('#thankYouMessageContainer').show();
    if(result)
    {
        $('#thankYouMessage').html(CommentSaveSuccessfulText);
    }
    else
    {
        $('#thankYouMessage').html(CommentSaveFailureText);
    }    
    $('#commentsContainer').removeClass('pane discussion discussionPost');
    $('#commentsContainer').addClass('pane discussion discussionComments');
    $('#profileNav').show();
}

function ShowRatingInfo(result)
{
    //debugger;
}

// Callback function invoked on success.
function onSuccess(result, userContext, methodName) 
{
    switch(methodName)
    {
        case "callWS":
        {
            Render(result,CommentsSectionId);
            break;
        }
        case "GetDownloadCount":
        {
            Render(result,CommentsSectionId);
            break;
        }
        case "AddPostComment":
        {
            ShowAddCommentThanks(result);
            break;
        }
        case "SkateReel2RateMedia":
        {
            ShowRatingInfo(result);
        }
        default:
        {
            Render(result);
            break;
        }
    }
}

function onSuccess_donothing(result)
{
    return true;
}

// Callback function invoked on failure.
function onFail(error, userContext, methodName)
{
    if(error !== null) 
    {
        var displayElement = $get(CommentsSectionId);
        displayElement.innerHTML = "An error occurred: " + error.get_message();
    }
}

function WriteVideo(videoPath, width, height, videoContainerId, swfFile, thumbUrl, scale, wmode, version, backgroundColor, postId, fileId, platformId, personaId)
{
    var flashvars = {
	videoURL: videoPath,
	playerWidth: "640",
	playerHeight: "480",
	playerPositionY: "-60",
	skinPlayerNormal: "/Themes/SkateReel2/Flash/SkinOverAll-Skate-NoCCHighLow.swf",
	skinPlayerFullScreen: "/Themes/SkateReel2/Flash/SkinOverAll-Skate-Fullscreen.swf",
	skinColorValue:"0x3d3e1d",
	pageName: "NA:US:EA:SKATE:SKATE2:MEDIA",
	s_account: "eaeacom,eagameskate2na,eaeabrandna,eaeacomna",
	cname: "eaeacom.112.2o7.net",
	namespace: "",
	align:"bottom",
	movieID: fileId,
	playerName: personaId,
	prop1:"No ID",
	prop2:"",
	prop3:"EA",
	prop4:"Skate",
	prop5:"GAME-SKATE2",
	prop6:"none",
	prop7:"",
	prop8:"",
	prop9:"",
	prop10:"FlashVideoPlayer",
	prop11:"",
	prop12:"",
	eVar1:"No ID",
	eVar2:"",
	eVar3:"GAME-SKATE2",
	event1:"",
	event2:"",
	event3:"",
	event4:"",
	event5:"",
	event40:"",
	align:"bottom",
	cdPath: "http://na.llnet.cdn.ea.com/u/crossdomain.xml,http://ll-100.ea.com/cem/u/f/GPO/crossdomain.xml",
	debug:"false"
	};

    var params = {
	base: "/Themes/SkateReel2/Flash/",
	allowScriptAccess: "always",
	allowFullScreen: "true",
	wmode: wmode,
	align:"bottom",
	bgcolor:"#000000"

	};
	var attributes = {	
	id: videoContainerId,
	name: videoContainerId,
	align:"bottom"
	};
	
    swfobject.embedSWF(swfFile, videoContainerId, width, height, "9.115.0", "/Themes/SkateReel2/Flash/expressInstall.swf", flashvars, params, attributes);
    
    UpdateSocialLinks(postId, fileId, platformId, personaId, 1);
}

function WriteImage(imagePath, width, height, containerId, postId, fileId, platformId, personaId)
{
    var container = '#' + containerId;
    var image = '<div id="' + containerId + '" style="VISIBILITY: visible"><img src="' + imagePath + '" width="' + width + '" height="' + height + '" ></div>';
    $(container).replaceWith(image);
    
    UpdateSocialLinks(postId, fileId, platformId, personaId, 2);
}

function UpdateBrowserTitle(personaName, media)
{
    document.title = 'Skate Reel 2 ' + media + ' by - ' + personaName + ' : ';
}

function UpdateSocialLinks(postId, fileId, platformId, personaId, mediaType)
{
    SkateReel2.Web.WebServices.SkateReelService.GetSocialLinks(postId, $("#video").html(), fileId, platformId, personaId, mediaType, ProcessSocialLinks, onFail);
}
function ProcessSocialLinks(result)
{
    $('#social_links').html(result);
}

function openModal(url, height, width)
{
    Telligent_Modal.Open(url, height, width, openModalSuccess);
}

function openModalSuccess(result)
{
    return true;    
}



function SendEmail(postId, accountId)
{
    SkateReel2.Web.WebServices.SkateReelService.SendAssetEmail(postId, accountId, $("#ctl00_toEmail").val(), SendEmailSuccess, onFail);
    $("#ctl00_sendEmailStatus").text('Sending....');
}
function SendEmailSuccess(result)
{
    if (result == 'success')
    {
        $("#ctl00_sendEmailStatus").text('Thank you, the email has been sent.');
        $("#ctl00_toEmail").val('');
    }
    else
    {
        $("#ctl00_sendEmailStatus").text('An error occurred sending the email.');
    }
        
}

function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {
	openPopup(url, name, width, height, status, scrollbars, moreProperties);
}
function openPopup(url, name, width, height, status, scrollbars, moreProperties) {
	var agent = navigator.userAgent.toLowerCase();
	var x, y = 0;
	if (screen) {
      x = (screen.availWidth - width) / 2;
      y = (screen.availHeight - height) / 2;
   }
   if (!status) status = '';

	// Adjust width if scrollbars are used (pc places scrollbars inside the content area; mac outside) 
	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;

	var properties = 'width=' + width + ',height=' + height + ',screenX=' + x + ',screenY=' + y + ',left=' + x + ',top=' + y + ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
   
	window.open(url, name, properties);
	return false;
	//ends
} 
