$(document).ready( function (){
    //initCufon();
    $('.default').show();
    xajax.loadingFunction =  showLoadingMessage;
    xajax.doneLoadingFunction = hideLoadingMessage;
    if (!$("#full_name").val()) $("#full_name").labelify({labelledClass: "labelHighlight"});
    if (!$("#user_email").val()) $("#user_email").labelify({labelledClass: "labelHighlight"});
    if (!$("#password").val()) $("#password").labelify({labelledClass: "labelHighlight"});
    $('.section_item').click(function() {
        var ed1 = tinyMCE.get('mceEditorContent');
        var ed2 = tinyMCE.get('mceEditorOtoContent');
        if(ed1.isDirty() || ed1.isDirty()) {
            if(!confirm("The changes you made will be lost if you navigate away from this page. Are you sure you want to leave this page?"))return false;
            ed1.isNotDirty = 1;
            ed2.isNotDirty = 1;
        }
	//left side
	$('.section_item_selected').addClass("section_item");
	$('.section_item').removeClass("section_item_selected");
	$(this).removeClass("section_item");
	$(this).addClass("section_item_selected");
	//right side
	$('.section_item_content').hide();
	$('#' + $(this).children(".item_link").attr("href")).show();
	return false;
    });
    $('.menu_item').live('click', function() {
	//left side
	$('.menu_item_selected').addClass("menu_item");
	$('.menu_item').removeClass("menu_item_selected");
	$(this).removeClass("menu_item");
	$(this).addClass("menu_item_selected");
	//right side
        var bid = $(this).children(".a_menu_link").attr("bid");
	$('.a_data' + bid).hide();
	$('#' + $(this).children(".a_menu_link").attr("href")).show();
	return false;
    });
    
    $('#enable_oto').change(function() {
        if ($('#enable_oto').is(':checked')) {
            $('#enable_oto').attr("checked","")    
            $('#enable_oto').val('1');
        }
        else {
            $('#enable_oto').removeAttr("checked"); 
            $('#enable_oto').val('0');  
        }
    });
    
    $(".image_frame").mouseover(function() {
        $(this).children(".button_apply").css( 'visibility', 'visible' );
    }).mouseout(function(){
        $(this).children(".button_apply").css( 'visibility', 'hidden' );
    });
});

function show_prompt(content, bid, post) {
    var name=prompt("Please enter name","");
    if (name!=null && name!="")
        xajax_processEditContent(name,content, bid, post);
}

function initCufon() {
    Cufon.replace(".title_box", {fontFamily: 'Leelawadee'});	
    Cufon.replace(".page-title", {fontFamily: 'Leelawadee'});	
}

function initUloadifyBanners(folder) {
    $('.upload_banner').uploadify({
        'uploader'  : '/tools/uploadify/uploadify.swf',
        'script'    : '/tools/uploadify/uploadify.php',
        'cancelImg' : '/tools/uploadify/cancel.png',
        'folder'    : folder,
        'auto'      : true,
        'buttonText': 'Add new',
        'fileDesc'  : 'Images (*.jpg; *.jpeg; *.gif; *.png)',
        'fileExt'   : '*.jpg;*.jpG;*.jPg;*.jPG;*.Jpg;*.JpG;*.JPg;*.JPG;*.jpeg;*.jpeG;*.jpEg;*.jpEG;*.jPeg;*.jpeg;*.jPeG;*.jPEg;*.jPEG;*.Jpeg;*.JpeG;*.JpEg;*.JpEG;*.JPeg;*.JPeG;*.JPEg;*.JPEG;*.gif;*.giF;*.gIf;*.gIF;*.Gif;*.GiF;*.GIf;*.GIF;*.png;*.pnG;*.pNg;*.pNG;*.Png;*.PnG;*.PNg;*.PNG',
        'chmod_file': 644,
        'chmod_dir' : 755,
        'onComplete': function(event, ID, fileObj, response, data) {xajax_updateBanners($('#blogid').val());},
        'hideButton': true,
        'wmode'     : 'transparent',
        'buttonImg' : '/images/buttons/add_new.png',
    });
}

function initUloadifyTicketAttachment(folder) {
    $('#ticket_attachment').uploadify({
        'uploader'  : '/tools/uploadify/uploadify.swf',
        'script'    : '/tools/uploadify/uploadify.php',
        'cancelImg' : '/tools/uploadify/cancel.png',
        'folder'    : folder,
        'auto'      : false,
        'buttonText': 'Add file',
        'fileDesc'  : 'Images (*.jpg; *.jpeg; *.gif; *.png)',
        'fileExt'   : '*.jpg;*.jpG;*.jPg;*.jPG;*.Jpg;*.JpG;*.JPg;*.JPG;*.jpeg;*.jpeG;*.jpEg;*.jpEG;*.jPeg;*.jpeg;*.jPeG;*.jPEg;*.jPEG;*.Jpeg;*.JpeG;*.JpEg;*.JpEG;*.JPeg;*.JPeG;*.JPEg;*.JPEG;*.gif;*.giF;*.gIf;*.gIF;*.Gif;*.GiF;*.GIf;*.GIF;*.png;*.pnG;*.pNg;*.pNG;*.Png;*.PnG;*.PNg;*.PNG',
        'chmod_file': 644,
        'chmod_dir' : 755,
        'onComplete': function(event, ID, fileObj, response, data) { },
        'onSelect'  : function(event,ID,fileObj) {$('#attach').val(fileObj.name);}
    });
}

function initUloadifyProduct(folder) {
    $('#product_attachment').uploadify({
        'uploader'  : '/tools/uploadify/uploadify.swf',
        'script'    : '/tools/uploadify/uploadify.php',
        'cancelImg' : '/tools/uploadify/cancel.png',
        'folder'    : folder,
        'auto'      : true,
        'buttonText': 'Add file',
        //'fileDesc'  : 'All (*.*)',
        //'fileExt'   : '*.*',
        'chmod_file': 644,
        'chmod_dir' : 755,
        'onComplete': function(event, ID, fileObj, response, data) {xajax_updateCurrentProduct($('#blogid').val())},
        'onSelect'  : function(event,ID,fileObj) {$('#attach').val(fileObj.name);},
        'hideButton': true,
        'wmode'     : 'transparent',
        'buttonImg' : '/images/buttons/add_new.png',
    });
}

function showLoadingMessage() {
    var sizes = getWindowSizes();
    var scrolls = getScrollXY();
    var h =  xajax.$('loading_message').style.height.substring(0,xajax.$('loading_message').style.height.length-2);
    var w =  xajax.$('loading_message').style.width.substring(0,xajax.$('loading_message').style.width.length-2);
    var xTop = sizes[1]/2 + scrolls[1] - h - 200;
    var xLeft = sizes[0]/2 + scrolls[0] - w/2;
    xajax.$('loading_message').style.top = xTop+'px';
    //xajax.$('loading_message').style.top = '150px';
    //xajax.$('loading_message').style.left = '700px';
    xajax.$('loading_message').style.left = xLeft+'px';
    xajax.$('loading_message').style.display='block';
}

function hideLoadingMessage() {
    xajax.$('loading_message').style.display = 'none';
}

function getScrollXY() {
    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return [ scrOfX, scrOfY ];
}

function getWindowSizes() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [ myWidth, myHeight ];
}

function nextTemplate() {
    var itemsCount = $("#template_select").size();
    var selectedIndex = $('#template_select').prop('selectedIndex');
    selectedIndex = selectedIndex + 2;
    $("#template_select :nth-child(" + selectedIndex + ")").attr("selected", "selected");
    $('#template_select').trigger("onchange"); 
    return false;
}

function prevTemplate() {
    var selectedIndex = $('#template_select').prop('selectedIndex');
    $("#template_select :nth-child(" + selectedIndex + ")").attr("selected", "selected");
    $('#template_select').trigger("onchange"); 
    return false;
}

function nextTemplateOto() {
    var itemsCount = $("#oto_template_select").size();
    var selectedIndex = $('#oto_template_select').prop('selectedIndex');
    selectedIndex = selectedIndex + 2;
    $("#oto_template_select :nth-child(" + selectedIndex + ")").attr("selected", "selected");
    $('#oto_template_select').trigger("onchange"); 
    return false;
}

function prevTemplateOto() {
    var selectedIndex = $('#oto_template_select').prop('selectedIndex');
    $("#oto_template_select :nth-child(" + selectedIndex + ")").attr("selected", "selected");
    $('#oto_template_select').trigger("onchange"); 
    return false;
}

function setCenter2(id_layer) {
    var scrollLeft, scrollTop;
    if (window.pageYOffset) {
            scrollTop = window.pageYOffset
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
            scrollTop = document.documentElement.scrollTop;
    }
    else if (document.body) {
            scrollTop = document.body.scrollTop;
    }
    if (window.pageXOffset) {
            scrollLeft=window.pageXOffset
    }
    else if (document.documentElement && document.documentElement.scrollLeft) {
            scrollLeft=document.documentElement.scrollLeft;
    }
    else if (document.body) {
            scrollLeft=document.body.scrollLeft;
    }
    var windowWidth, windowHeight;
    if (window.innerWidth) {
            windowWidth=window.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
            windowWidth=document.documentElement.clientWidth;
    }
    else if (document.body) {
            windowWidth=document.body.offsetWidth;
    }
    if (window.innerHeight) {
            windowHeight=window.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight){
            windowHeight=document.documentElement.clientHeight;
    }
    else if (document.body){
            windowHeight=document.body.clientHeight;
    }
    var layerWidth, layerHeight;
    layerWidth=document.getElementById(id_layer).offsetWidth;
    layerHeight=document.getElementById(id_layer).offsetHeight;

    var delta;
    delta = windowHeight - layerHeight;
    if (delta < 0) delta=0;
    document.getElementById(id_layer).style.top = (delta)/2 + scrollTop + 'px';
    document.getElementById(id_layer).style.left = (windowWidth - layerWidth)/2 + 'px';
}



jQuery.fn.labelify=function(settings){settings=jQuery.extend({text:"title",labelledClass:""},settings);var lookups={title:function(input){return $(input).attr("title");},label:function(input){return $("label[for="+input.id+"]").text();}};var lookup;var jQuery_labellified_elements=$(this);return $(this).each(function(){if(typeof settings.text==="string"){lookup=lookups[settings.text];}else{lookup=settings.text;};if(typeof lookup!=="function"){return;}
var lookupval=lookup(this);if(!lookupval){return;}
$(this).data("label",lookup(this).replace(/\n/g,''));$(this).focus(function(){if(this.value===$(this).data("label")){this.value=this.defaultValue;$(this).removeClass(settings.labelledClass);}}).blur(function(){if(this.value===this.defaultValue){this.value=$(this).data("label");$(this).addClass(settings.labelledClass);}});var removeValuesOnExit=function(){jQuery_labellified_elements.each(function(){if(this.value===$(this).data("label")){this.value=this.defaultValue;$(this).removeClass(settings.labelledClass);}})};$(this).parents("form").submit(removeValuesOnExit);$(window).unload(removeValuesOnExit);if(this.value!==this.defaultValue){return;}
this.value=$(this).data("label");$(this).addClass(settings.labelledClass);});};
