﻿$(function() {
    var searchUrl;
    $('#QuickShareInput').click(function() {
        $(this).val('');
    });

    $('#SearchInput').click(function() {
        $(this).val('');
    });

    $('#SiteSearch').click(function() {
        var SearchVal = trim($('#SearchInput').val());
        if (SearchVal != '' && SearchVal != 'Quick Search') {
            var wordArray = $('#SearchInput').val().match(/\w+/g);
            var concatKeys = "";
            concatKeys = wordArray.join(",");
            var searchUrl = '/Pages/Search/Results.aspx?search=' + concatKeys + '&start=&end=&page=1&renew=false';
            window.location.href = searchUrl;
        }
    });

    $('#SearchInput').keypress(function(e) {
        code = e.keyCode ? e.keyCode : e.which;
        if (code.toString() == 13) {
            $('#SiteSearch').trigger("click");
        }
    });


    $('#QuickShareButton').click(function() {
        //Background
        var browser = $.browser;
        if (browser.msie) {
            $('#ShareOverlay').addClass('IEShareOverlay');
        }
        else {
            $('#ShareOverlay').addClass('ShareOverlay');
        }

        $('#ShareOverlay').css({ 'height': $(document).height(), 'width': $(document).width(), 'display': 'block' });

        //Display box
        $('#ShareContentBox').css({ 'display': 'block', 'left': (($(document).width() / 2) - 350) });

        //IE7 Fix
        $('#OverlayHeaderRow').css('display', 'none');
        $('#VideoAudioContent').css('display', 'none');

        var fakeHeader = "<table cellspacing='0' cellpadding='0' width='100%'><tr class='ShareHeaderRow'><td class='HeaderRowPaddingLeft'>Code</td><td>Name</td><td>Hi</td><td>Lo</td><td>Close</td><td>Spot</td><td><div style='float: right;' onclick='javascript:CloseWindow()' id='ShareContentClose'><img alt='Close' src='/Images/MasterPage/x.png'/></div></td></tr></table><img src='/Images/MasterPage/loading.gif'/>";
        $('#ShareContent').html(fakeHeader).addClass('CenterAlign'); ;
        $('#ShareContent').css('overflow', 'scroll');

        $.ajax({
            type: 'POST',
            url: '/FMService.asmx/QuickShareSearch',
            dataType: 'html',
            data: "{'shareCode': '" + $('#QuickShareInput').val() + "' }",
            contentType: "application/json; charset=utf-8",
            error: function(xhr, desc, exceptionobj) {
                LogError(xhr);
            },
            success: function(json) {
                $('#ShareContent').removeClass('CenterAlign');
                $('#ShareContent').html(json);

                //IE7 Fix
                if (browser.msie) {
                    $('.SearchTable').switchClass('SearchTable', 'SearchTableSetWidth');
                }
            }
        });
    });

    $('#QuickShareInput').keypress(function(e) {
        code = e.keyCode ? e.keyCode : e.which;
        if (code.toString() == 13) {
            $('#QuickShareButton').trigger("click");
        }
    });

    $('#ArticleVideoViewer').click(function() {
       
        var browser = $.browser;
        if (browser.msie) {
            $('#ShareOverlay').addClass('IEShareOverlay');
        }
        else {
            $('#ShareOverlay').addClass('ShareOverlay');
        }


        //IE7 fix
        $('#ShareContent').css('display', 'none');

        //Overlay
        $('#ShareOverlay').css({ 'height': $(document).height(), 'width': $(document).width(), 'display': 'block' });
        //Display box
        $('#ShareContentBox').css({ 'display': 'block', 'left': (($(document).width() / 2) - 350) });
        var fakeHeader = "<table cellspacing='0' cellpadding='0' width='100%'><tr class='ShareHeaderRow'><td class='HeaderRowPaddingLeft'>Video</td><td><div style='float: right;' onclick='javascript:CloseWindow()' id='ShareContentClose'><img alt='Close' src='/Images/MasterPage/x.png'/></div></td></table>"; //<img src='/Images/MasterPage/loading.gif'/>
        $('#ShareHeader').html(fakeHeader);

       
        $.ajax({
            type: 'POST',
            url: '/FMService.asmx/GetArticleVideoHtml',
            dataType: 'json',
            data: "{'articleId': '" + getParameterByName('id') + "' }",
            contentType: "application/json; charset=utf-8",
            error: function(xhr, desc, exceptionobj) {
                LogError(xhr); 
            },
            success: function(json) {
               
                $('#VideoAudioContent').html(json.d);

                for (var i = 0; i < ArticleVideos.length; i++) {
                    CreateFlash(ArticleVideos[i].VideoId, ('VideoObject' + (i + 1)), "video");
                }
            }
        });
    });

    $('#ArticleAudioViewer').click(function() {
        var browser = $.browser;
        if (browser.msie) {
            $('#ShareOverlay').addClass('IEShareOverlay');
        }
        else {
            $('#ShareOverlay').addClass('ShareOverlay');
        }

        //IE7 fix
        $('#ShareContent').css('display', 'none');

        //Overlay
        $('#ShareOverlay').css({ 'height': $(document).height(), 'width': $(document).width(), 'display': 'block' });
        //Display box
        $('#ShareContentBox').css({ 'display': 'block', 'left': (($(document).width() / 2) - 350) });
        var fakeHeader = "<table cellspacing='0' cellpadding='0' width='100%'><tr class='ShareHeaderRow'><td class='HeaderRowPaddingLeft'>Audio</td><td><div style='float: right;' onclick='javascript:CloseWindow()' id='ShareContentClose'><img alt='Close' src='/Images/MasterPage/x.png'/></div></td></table>"; //<img src='/Images/MasterPage/loading.gif'/>
        $('#ShareHeader').html(fakeHeader);

        $.ajax({
            type: 'POST',
            url: '/FMService.asmx/GetArticleAudioHtml',
            dataType: 'json',
            data: "{'articleId': '" + getParameterByName('id') + "' }",
            contentType: "application/json; charset=utf-8",
            error: function(xhr, desc, exceptionobj) {
                LogError(xhr);
            },
            success: function(json) {


                $('#VideoAudioContent').html(json.d);

                for (var i = 0; i < ArticleAudio.length; i++) {
                    CreateFlash(ArticleAudio[i].AudioId, ('VideoObject' + (i + 1)), "audio");
                }
            }
        });
    });

});

function CloseWindow() {
    
    $('#ShareOverlay').css('display', 'none');
    $('#ShareContentBox').css('display', 'none');
    $('#ShareHeader').html('');
    $('#ShareContent').html('');
    //SWFObject replaces the div VideoAudioContent
    $('#VideoAudioWorker').html('<div id="VideoAudioContent"></div>');

    //IE7 Fix
    $('#OverlayHeaderRow').css('display', 'block');
    //IE fix
    $('#VideoAudioContent').css('display', 'block');
    $('#ShareContent').css('display', 'block');
    
}

function CreateFlash(vid, contentHolder,type) {
    //Flash
    var TheTitle = document.getElementById('hidVideoTitle').value;
    if (type == "video") {
        var flashvars = {
            height: "173",
            width: "223",
            file: "http://multimedia.avusa.co.za/flvideo/" + vid + ".flv",
            image: "http://multimedia.avusa.co.za/thumb/1_" + vid + ".jpg",
            skin: '',//http://test.businessday.co.za/Flash_Player/beelden.xml',
            'logo.file': '',
            'logo.position': 'top-right',
            'logo.hide': 'false',
            abouttext: 'Financial Mail',
            aboutlink: 'http://www.fm.co.za',
            plugins: 'dcinstream,gapro-1',
            'gapro.accountid': 'UA-5210359-21',
            mediaid: vid,
            title: TheTitle,
            'gapro.trackstarts': 'true',
            'gapro.trackpercentage': 'true',
            'gapro.tracktime': 'true',
            'gapro.idstring': '||mediaid||-||title||',
            'googima.ad.tag': 'http://ad.za.doubleclick.net/pfadx/n5963.financialmail/videoplayer;sz=1x1000;tile=1;dcmt=text/xml;ord=1234567890?', //http://ad.za.doubleclick.net/pfadx/n5963.businessday/VideoPlayer;sz=300x250;tile=1,
            'googima.ad.position': 'pre',
            'googima.allowadskip': 'true',
            'googima.usestaticmessage': 'true',
            'googima.admessagestatic': 'Click the X to close this advert'
        };
    }
    else {
        var flashvars = {
            height: "173",
            width: "223",
            file: "http://multimedia.avusa.co.za/audio/" + vid + ".mp3",
            image: "http://multimedia.avusa.co.za/images/psd-AVUSA-podcast.jpg"
        };
    }
    
    var params = {
        allowfullscreen: "true",
        wmode: "transparent",
        allowScriptAccess: "always"
    };

    var attributes = {};

    swfobject.embedSWF("http://www.businessday.co.za/Flash_Player/player-licensed.swf", contentHolder, "223", "173", "8", "", flashvars, params, attributes);
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return document.getElementById('hidVideosId').value;
    else
        return results[1];
}

function trim(s) {

    s = s.replace(/(^\s*)|(\s*$)/gi, "");

    s = s.replace(/[ ]{2,}/gi, " ");

    s = s.replace(/\n /, "\n");

    return s;

}


