﻿/*
 *   Balderton Project
 *       
 *   This common.js file was created to provide some common functionallities.
 *   like blocking UI , user friendly messagebox or others..
 *
 *   Created date : 10/09/2008 cem@skyron.co.uk
 *
 */


 /*
 *   block ui to tell user that operation status like saying data saved successfuly or error occured ....    
 */
function __blockUI(html) {
    $.blockUI({ message: "<div style=\"padding:10px;font-weight:bold;\">" + html + "</div>" });
    setTimeout(jQuery.unblockUI, 800); // after we tell user what happened, unblock the UI.
}

function __ajaxCall(url , data , callback) {
    $.ajax({
        url: url
        , type: 'post'
        , data: data
        , error: function() {
            alert('3rr0r 0ccured! Please refresh the page!');
          }
        , success: callback  
    });
}

var ajaxObject;
function __ajaxCallXml(url, data, callback) {
    //window.status = ajaxObject;
    ajaxObject = $.ajax({
        url: url
        , type: 'post'
        , data: data
        , global: false
        , dataType: 'xml'
        , error: function() {
            alert(url + '\n\n3rr0r 0ccured! Please refresh the page!');
        }
        , success: callback        
    });
}




$('#loading').ajaxComplete(function() {
    $(this).hide();    
    //return false;
});

$(function() {
    // change message border
    $.blockUI.defaults.css.border = '1px solid #bdbdbd';
    //$.blockUI.defaults.css.cursor = '';

    // make fadeOut effect shorter 
    $.blockUI.defaults.fadeOut = 200;
});


function __tinymce(id,callback,resizeEnabled) {
    tinyMCE.init({
         mode: "exact"
        , theme: "advanced"
        //, editor_selector : id
        //plugins : "safari,spellchecker,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,pagebreak,imagemanager,filemanager",
		    , plugins: "save,media,spellchecker"
		    , theme_advanced_buttons1: "save,bold,italic,underline,link,unlink,undo,redo,media,code"
		    , theme_advanced_buttons2: ""
		    , theme_advanced_buttons3: ""
        //, theme_advanced_buttons3: "save"
		    , save_enablewhendirty: false
		    , save_onsavecallback: callback
		    , theme_advanced_toolbar_location: "top"
		    , theme_advanced_toolbar_align: "left"
		    , theme_advanced_resizing: resizeEnabled
            , theme_advanced_statusbar_location: "bottom"
            , apply_source_formatting: true
            , spellchecker_languages: "+English=en"
            , content_css: "/themes/standart/css/03.css"

        //		    theme_advanced_buttons1_add_before : "save",
        //		    theme_advanced_buttons1_add : "fontselect,fontsizeselect",
        //		    theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,separator,forecolor,backcolor",
        //		    //theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
        //		    //theme_advanced_buttons3_add_before : "tablecontrols,separator",
        //		    //theme_advanced_buttons3_add : "emotions,iespell,media,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
        //		    //theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,spellchecker,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
        //		    theme_advanced_toolbar_location : "top",
        //		    theme_advanced_toolbar_align : "left",
        //		    theme_advanced_statusbar_location : "bottom",
        //		    //content_css : "/example_data/example_full.css",
        //	        //plugin_insertdate_dateFormat : "%Y-%m-%d",
        //	        //plugin_insertdate_timeFormat : "%H:%M:%S",
        //		    //external_link_list_url : "example_data/example_link_list.js",
        //		    //external_image_list_url : "example_data/example_image_list.js",
        //		    //flash_external_list_url : "example_data/example_flash_list.js",
        //		    //template_external_list_url : "example_data/example_template_list.js",
        //		    theme_advanced_resize_horizontal : false,
        //		    theme_advanced_resizing : true,
        //		    apply_source_formatting : true,
        //		    spellchecker_languages : "+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv"
    });


}




function OTPopulateTinyMCE(id, callback, forceP) {
    tinyMCE.init({
        mode: "exact"
        , elements: id
        , theme: "advanced"
        , cleanup: true
        //, editor_selector : id
        //plugins : "safari,spellchecker,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,pagebreak,imagemanager,filemanager",
		    , plugins: "save,spellchecker"
		    , theme_advanced_buttons1: "save,bold,italic,underline,link,code"
		    , theme_advanced_buttons2: ""
		    , theme_advanced_buttons3: ""
        //, theme_advanced_buttons3: "save"
		    , save_enablewhendirty: false
		    , save_onsavecallback: callback
		    , theme_advanced_toolbar_location: "top"
		    , theme_advanced_toolbar_align: "left"
		    , theme_advanced_resizing: true
            , theme_advanced_statusbar_location: "bottom"
            , apply_source_formatting: true
            , spellchecker_languages: "+English=en"
        //, content_css: "/themes/standart/css/01.css,/themes/standart/css/033.css"
            , forced_root_block: forceP
            , force_br_newlines: !forceP
            , force_p_newlines: forceP
            , setup: function(ed) {
                ed.addButton('h1', { image: '/themes/standart/images/tinyMce/h1.gif', onclick: function() { ed.selection.setContent('<h1>' + ed.selection.getContent() + '</h1>'); } })
                ed.addButton('h2', { image: '/themes/standart/images/tinyMce/h2.gif', onclick: function() { ed.selection.setContent('<h2>' + ed.selection.getContent() + '</h2>'); } })
            }

    });
}




function FixPlusChar(str) {
    var d = str.replace(/\+/g, '[phone]');    
    return d;
}



function playVideo(src, containerID) {
    
    var s1 = new SWFObject('/media/player/player.swf', 'ply', '420', '256', '9', '#ffffff');
    s1.addParam('allowfullscreen', 'true');
    s1.addParam('allowscriptaccess', 'always');
    s1.addParam('wmode', 'opaque');
    s1.addParam('flashvars', 'file=/media/videos/' + src + '&image=/media/videos/' + src.replace('flv', 'png') + '&plugins=embed-1');
    //s1.addParam('flashvars', 'file=http://balderton.clients.skyron.co.uk/media/homeflash/video1.flv');
    s1.write(containerID);
}


// tinymce plugin
tinymce.create('tinymce.plugins.DesignPlugin', {
    createControl: function(n, cm) {
        switch (n) {
            case 'designListBox':
                var mlb = cm.createListBox('designListBox', {
                    title: 'Select',
                    onselect: function(v) {
                        //tinyMCE.activeEditor.windowManager.alert('Value selected:' + v);
                        var content = tinyMCE.activeEditor.selection.getContent();

                        switch (v) {
                            case 'makeGray':
                                tinyMCE.activeEditor.selection.setContent('<p class=\"au-p\">' + content + '</p>');
                                return;
                            case 'seperator':
                                tinyMCE.activeEditor.selection.setContent(content + '<hr class=\"sep\" />');
                                return;
                        }
                    }
                });

                // Add some values to the list box
                mlb.add('Make grey bg', 'makeGray');
                mlb.add('Seperator', 'seperator');


                // Return the new listbox instance
                return mlb;

            case 'mysplitbutton':
                var c = cm.createSplitButton('mysplitbutton', {
                    title: 'My split button',
                    image: 'img/example.gif',
                    onclick: function() {
                        tinyMCE.activeEditor.windowManager.alert('Button was clicked.');
                    }
                });

                c.onRenderMenu.add(function(c, m) {
                    m.add({ title: 'Some title', 'class': 'mceMenuItemTitle' }).setDisabled(1);

                    m.add({ title: 'Some item 1', onclick: function() {
                        tinyMCE.activeEditor.windowManager.alert('Some  item 1 was clicked.');
                    }
                    });

                    m.add({ title: 'Some item 2', onclick: function() {
                        tinyMCE.activeEditor.windowManager.alert('Some  item 2 was clicked.');
                    }
                    });
                });

                // Return the new splitbutton instance
                return c;
        }

        return null;
    }



});
