var scriptName = "sidebar.js";
var fn_play_video = function(){
    var s = $("#modal").attr("style") + ';width:930px';
    $("#modal").attr("style", s);
    
    if ($(".swf").size() > 0) {
        $(".swf").each(function(){
            var videoPlayer = $("#videoplayer")[0];
            var videoMain = $(this).attr("rel");
            if ((videoMain == '') && ($(".videoMain").size() > 0)) {
                videoMain = $(".videoMain:eq(0)").attr('href');
            }
            var path = $(this).attr("path");
            var w = ($(this).attr("width") == undefined) ? 400 : $(this).attr("width");
            var h = ($(this).attr("height") == undefined) ? 340 : $(this).attr("height");
            
            $(this).media({
                flashVersion: '9',
                width: w,
                height: h,
                params: {
                    wmode: 'transparent'
                },
                flashvars: {
                    flvsource: videoMain,
                    path: path
                }
            });
            
        });
    }
    
    $("a.videoLink").unbind('click').bind('click', function(){
        var videoPlayer = $("#videoplayer")[0];
        var flv = $(this).attr('rel');
        var href = $(this).attr('href');
        
        videoPlayer.playVideo(flv);
        changeVideoContent(href);
        
        fn_play_video();
        return false;
    });
    
    $("#lightboxContainer_O .lbOn").detDialog({});
    $("#lightboxBody_I .videoContainer .linkMoreInfo").each(function(){
        $(this).html('Text version of this video');
    });
};

var callbacktest = function(){
    alert('callbacktest launched');
}; // end function
var changeVideoContent = function(url){

    var events;
    $.ajax({
        url: url,
        async: false,
        success: function(data){
            if (data.error) {
                alert("Error while retreiving events: " + data.error[0].Text);
            }
            else {
                var newData = cleanUpVideoData(data);
                $(".videoContainer .colTwo").html(newData);
                fn_play_video()
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            alert('Error:' + XMLHttpRequest);
        }
    });
    return events;
}

var cleanUpVideoData = function(responseText){
    var l = responseText.length;
    var n = responseText.indexOf("colTwo");
    var div = '';
    var R = '';
    var p = 0;
    var c = '';
    if (n > 0) {
        c = responseText[n];
    }
    for (i = n; (i > 0) && (c != '<'); i--) {
        c = responseText.substr(i, 1);
    }
    n = i + 1;
    var c = responseText.substr(n, 1);
    div = responseText.substr(n);
    do {
        i = div.indexOf('div');
        if (i > 0) {
            c = div.substr(i - 1, 1);
            if (c == '<') {
                p++;
            }
            else 
                if (c == '/') {
                    p--;
                }
            R = R + div.substr(0, i + 4);
            div = div.substr(i + 4);
        }
        else {
            p = -1;
        }
    }
    while (p > 0);
    n = i;
    return R;
}

var notImplemented = function(){

    //This part is only for the toolbar which they style is for <li>
    $(".notImplemented").parents("li").each(function(){
        $(this).attr("style", "text-decoration:none");
    });
    var notImp = ($(".notImplemented").size()>0)?$(".notImplemented").unbind():false;
	if(notImp){
	    if (notImp.attr('href')) {
	        notImp.attr('href', 'javascript:void(0)');
	    }
	    notImp.attr("disabled", "disabled");
	    notImp.attr('title', 'This function has not yet been released');
	    notImp.tooltip();
	}
}


var ObjectPosition = function(obj){
    var curleft = 0;
    var curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
        while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}

var setPosition = function(){
	var refID = 'sideBarRef';
	if ($("div.banner_O:eq(0)").size() > 0) {
		$("div.banner_O:eq(0)").attr("id", refID);
	}else{
		refID = 'header_I';
	}
    var P = ObjectPosition(document.getElementById(refID));
    var pl = P[0];
    var pw = $("#"+refID).width();
    var sw = $(".sideBarCover .sideBar").width();
    var L = pl + pw - sw;
    $(".sideBarCover .sideBar").attr("style", "left:" + L + "px");
}

var markAsDraft = function(){
	$(".isDraft a").each(function(){
		$(this).html('(('+$(this).html()+'))');
	});
}

$(document).ready(function(){
	markAsDraft();
    $(".sideBar .video .lbOn").unbind().detDialog({
        callback: fn_play_video
    });

    $(".sideBar .button .lbOn").unbind().detDialog({});

	$(".login_I .logout").detDialog({});
	$(".sideBar  .button .lbOn").detDialog({});
    
	$(".sideBar a.helpLink").attr("target", "_blank");
    $("#addsection  .addSection").unbind().detDialog({
        callback: fn_form_pa004
    });
    
    if (window.notImplemented) {
        notImplemented();
    }
    $(".sideBarCover").appendTo("body");
    $(window).bind('resize', setPosition);
    setPosition();
    parseTitle();

    if (window.BANNER_PREVIEW_MODE && BANNER_PREVIEW_MODE == true) {
		if(window.modePreview){
			modePreview();
		}
	}
	
})

var parseTitle = function(){
    $(".draft_title").each(function(){
        var text = $(this).text();
        
        var MAX_TITLE_LN = 15;
        var REDUCE_TITLE_LN = 12;
        if (text.length > MAX_TITLE_LN) {
            text = text.substr(0, REDUCE_TITLE_LN) + "...";
        }
        
        $(this).text(text);
        
    })
}

/*Add section functions*/
var fn_form_pa004_validate = function(){
  var form = $("form[@name='form_pa004']");
  return form.validate({
      error:'PA004E1',
      newSubpage: { required:true, printable:true }
  });
}; // end function

var fn_form_pa004 = function(){
    if (window.callLog) 
        callLog(scriptName + ",fn_form_pa004");
    var form = $("form[@name='form_pa004']");
    form.unbind().bind('submit', function(event){
        if (window.callLog) 
            callLog(scriptName + ",fn_form_pa004,submit");
        form.ajaxSubmit({
            dataType: 'xml',
            beforeSubmit: function(formData, jqForm, options){
                // do validation here
                if (window.fn_form_pa004_validate) {
                    return fn_form_pa004_validate();
                } // end if
            },
            success: function(response, status){
                if ($("draft_id", response).length > 0) {
                    var draft_id = $("draft_id", response).text()
                    window.location = draft_id + ".draft";
                } // end if
                else {
                    $.ajax({
                        url: action_form_pa004,
                        success: function(response){
                            $.detDialog({
                                html: response
                            });
                        }
                    });
                } // end if
                /*var modalCss = jqForm.getDialogmeta();
                 $.detDialog({html:response,width:'900px'});*/
            }
        });
        return false;
    });
    $(":submit", form).unbind().bind('click', function(event){
        if (window.callLog) 
            callLog(scriptName + ",fn_form_pa004,submit.click");
        form.trigger('submit');
        event.preventDefault();
    });
}; // end function