﻿var isEdited = false;
var isLiveContent = false;

$(document).ready(function(){  
    var possibleParams = new Array('quality', 'scale', 'wmode', 'bgcolor', 'base', 'flashvars', 'allowScriptAccess');

    $('embed[type=application/x-shockwave-flash]').each(function() {
        var id = $(this).attr('id');
        if (id.length > 0) {
            var noembed = $('.noembed[rel=' + id + ']');
            var params = new Array();

            for (var i = 0; i < possibleParams.length; i++)
                if ($(this).attr(possibleParams[i]) != undefined)
                    if ($(this).attr(possibleParams[i]).length > 0)
                        params.push(new param(possibleParams[i], $(this).attr(possibleParams[i])));

            var obj = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + $(this).attr('width') + '" height="' + $(this).attr('height') + '" id="' + id + '" class="' + $(this).attr('class') + '">';
            obj += '<param name="movie" value="' + $(this).attr('src') + '" />';

            for (var i = 0; i < params.length; i++)
                obj += '<param name="' + params[i].name + '" value="' + params[i].val + '" />';

            obj += '<!--[if !IE]>--><object type="application/x-shockwave-flash" data="' + $(this).attr('src') + '" width="' + ($(this).attr('width') != undefined ? $(this).attr('width') : '') + '" height="' + ($(this).attr('height').length != undefined ? $(this).attr('height') : '') + '">';

            for (var i = 0; i < params.length; i++)
                obj += '<param name="' + params[i].name + '" value="' + params[i].val + '" />';

            obj += '<!--<![endif]-->';

            if ($(noembed).length > 0) {
                obj += $(noembed).html();
                $(noembed).remove();
            }
            obj += '<!--[if !IE]>--></object><!--<![endif]-->';

            obj += '</object>';

            $(this).replaceWith(obj);

            if (id.length > 0) {
                swfobject.registerObject(id, "9.0.0");
                $('#' + id).css('visibility', 'visible');
           }
       }
   });
            
    if ($("#cms").length != 0) {
        var isPublished = $('input[type=hidden][id*=fldIsPublished]').val();
        
        $("#cms").load("/CMS/ToolbarContainer.aspx?isPublished=" + isPublished);
        
        if (isEdited == true) {
             $('.pnlIsPublished').show();
        }
        
        var testing123 = $('.pnlIsPublished').length;
        
        if (isPublished == "True") {
            $('.pnlIsPublished').hide();
        }
        else {
            $('.pnlIsPublished').show();
        }
    }
         
    $('.contentDiv').each(function()
    {
        var onClickID = $(this).attr('id');
        var onClickString = "$('#" + onClickID + "').click();";
        var contentString = "<a href=\"javascript:void(0);\" onclick=\"" + onClickString + "\"><img src='/CMS/Styles/edit.png' border='0' alt='Click to Edit' /></a>";
          
        $(this).qtip(
        {
            content: contentString, // Give it some content
            position: 'topRight', // Set its position
            hide: {
                fixed: true // Make it fixed so it can be hovered over
            },
            style: {
                padding: '5px 5px',
                border: {
                    width: 0,
                    radius: 0,
                    color: 'Transparent'                
                },
                background: 'Transparent'
            }
        });
    });      
    
    $('body').supersleight({shim: '/images/blank.gif'});
});

function param(name, val) {
    this.name = name;
    this.val = val;
}

function GetSearchResults(nIsLiveContent) {
    var searchString = $('.searchTextBox').val();
    isLiveContent = nIsLiveContent;
    CMSService.ParseSearchString(searchString, OnMainSearchSucceed, OnQueryFailed);
}

function OnMainSearchSucceed(msg) {
    if (isLiveContent) {
        window.location = "/search?" + msg;
    }
    else {
        window.location = "/edit/search?" + msg;
    }
}

function GetRedirectUrl(sectionID) {

}

function OnSearchFail(msg) {

}

function SetTextBox(textForBox, friendlyUrl) {
    $(".watermark").val(textForBox);
     $("#teamSearchResult").hide();
}

function ShowUnpublished()
{
    isEdited = true;
    $('.pnlIsPublished').show();
}
    
function HideUnpublished()
{
    $('.pnlIsPublished').hide();
}
                        
function RegisterUser()
{
    var exp = /^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
    var email = $('input[type=text][id*=txtRegister]').val();
    
    if (email == '') {
        $('#ErrorLabel').text('Enter email address');
        $('#ErrorLabel').css('display', 'block');
        $('#ErrorLabel').css('color', 'Red');
    }
    else
    {    
        if (exp.test(email)) {
            CMSService.RegisterEmail(email, OnRegisterSucceeded, OnRegisterFailed); 
        }
        else
        {
            $('#ErrorLabel').text('Enter valid address');
            $('#ErrorLabel').css('display', 'block');
            $('#ErrorLabel').css('color', 'Red');
        }    
    }
}

function SubmitQuery() {
    if (Page_ClientValidate("Contact")) {
        var queryType = $('.ddlQueryType').val();
        var name = $('input[type=text][id*=txtName]').val();;
        var telephone = $('input[type=text][id*=txtTelephone]').val();;
        var email = $('input[type=text][id*=txtEmail]').val();;
        var business = $('input[type=text][id*=txtBusiness]').val();;
        var message = $('.txtMessage').val();;
    
        CMSService.SubmitQuery(queryType, name, telephone, email, business, message, OnQuerySucceeded, OnQueryFailed); 
    }
}

function SetCellOverStyle(overlay){
    $('.' + overlay).removeClass("cellOff");
    $('.' + overlay).addClass("cellOver");
}
    
function SetCellNormalStyle(overlay){
    $('.' + overlay).removeClass("cellOver");
    $('.' + overlay).addClass("cellOff");
}

function SetSubSectionIDLabel(subSectionID) {
    $('.OverlaySelected').val(subSectionID);
}

function OnCloseDown(imgID) {
    $("#menu").toggle();
    $("#cmsAreaDiv").toggle();
}

function OnCloseOver(img) {
    $(img).attr("src", "../App_Themes/Default/images/CloseOver.gif");
}

function OnCloseOut(img) {
    $(img).attr("src", "../App_Themes/Default/images/CloseOut.gif");
}

function OnSucceeded(msg) {
    var positionIndex = $('.PositionIndex').val();
    var content = $('.HtmlContent').val();
    
    $("#ltrHoverOverlay" +  $('.PositionIndex').val()).html($('.HtmlContent').val()); 
    ShowUnpublished(); 
}

function OnRegisterSucceeded(msg) {
    if (msg == '') {
        $('#ErrorLabel').text('Thank you');
        $('#ErrorLabel').css('display', 'block');
        $('#ErrorLabel').css('color', 'green');
    }
    else {
        $('#ErrorLabel').text('Already registered');
        $('#ErrorLabel').css('display', 'block');
        $('#ErrorLabel').css('color', 'Red');
    }
}

function OnQuerySucceeded(msg) {
    var name = $('input[type=text][id*=txtName]').val('');
    var telephone = $('input[type=text][id*=txtTelephone]').val('');
    var email = $('input[type=text][id*=txtEmail]').val('');
    var business = $('input[type=text][id*=txtBusiness]').val('');
    var message = $('.txtMessage').val('');
    
    $('#lblResult').text('Thank you for your enquiry');
}

function OnRegisterFailed() {

}

function OnQueryFailed(msg) {

}

function OnFailure(msg) {
     $(".overlay" +  $('.PositionIndex').val()).html(msg.get_message());
}

function GoToLive() {
    var nLocation = window.location.pathname;
    
    nLocation = nLocation.replace('/edit/', '/');
    window.location = nLocation;
}

function OpenEditorWindow(sectionID, sectionElementID, positionIndex, width) {    
    var sd = $('input[type=hidden][id*=fldSectionID]').length;
    var dfdfg =  $('input[type=hidden][id*=fldSectionID]').length;
    
    $('input[type=hidden][id*=fldSectionID]').val(sectionID);
    $('input[type=hidden][id*=fldSectionElementID]').val(sectionElementID); 
    $('input[type=hidden][id*=fldPositionIndex]').val(positionIndex);
    $('input[type=hidden][id*=fldWidth]').val(width);
    
    $("#cmsEditorDiv").html("<iframe id='editorIframe' src='/CMS/ElementEditor.aspx?sectionID=" + sectionID + "&sectionElementID=" + sectionElementID + "&positionIndex=" + positionIndex + "&width=" + width + "' width='872' height='501' style='z-index: 100;' scrolling='no' frameborder='0' marginwidth='0' marginheight='0'></iframe>");
    $('#cmsEditorDiv').show();
}

function GetOfflineContent(sectionElementID) {
    CMSService.GetOfflineContent(sectionElementID, OnGetOfflineSucceed, OnFailure);
}

function CloseEditor() {
    $("#cmsEditorDiv").hide();
}

function OnGetOfflineSucceed(msg) {
    var posIndex = $('input[type=hidden][id*=fldPositionIndex]').val();
    
    $('#ltrHoverOverlay' + posIndex).html(msg);
}

function InsertSubscriber() {
    if (Page_ClientValidate("signup")) {
        var firstname = $('input[type=text][id*=txtFirstname]:value').val();
        var lastname = $('input[type=text][id*=txtLastname]:value').val();
        var company = $('input[type=text][id*=txtCompany]:value').val();
        var email = $('input[type=text][id*=txtEmailAddress]:value').val();
        var telephone = $('input[type=text][id*=txtTelephone]:value').val();
        var companyAddress = $("textarea[id*=txtCompanyAddress]:value").val();
        var postcode = $('input[type=text][id*=txtCompanyPostcode]:value').val();   
    
        CMSService.InsertSubscriber("0", firstname, lastname, company, email, telephone, companyAddress, postcode, InsertSubscriberSucceed, SubscriberFail)
    }
}

function InsertSubscriberSucceed(msg) {
    if (msg == "false") {
        $('.lblEmailInUse').show();
    }
    else {
        $(".signupBoxes").hide();
        $(".signupResult").show();
    }
}

function SubscriberFail(msg) {
    var ds = "error";
    ds = "ww";
}

function Logout() {
    window.location = "/logout";
}
