/// <reference path="jquery/jquery-vsdoc.js" />

/*function font_size_adjust(doc) {
    doc = doc || document;
    var font_size_ratio = Math.floor(Math.max($(window).width(), 1024) / 16);
    try
    {
	    $("body", doc).css('font-size', font_size_ratio + '%');
        if($.browser.msie && $.browser.version.match(/^8\./))
            $("select", doc).css('font-size', '1em'); // fix for IE8
    }
    catch(e){
        setTimeout(function() {
            font_size_adjust(doc);
        }, 100);
    }
    return false;
}

$(window).resize(function($) {
    font_size_adjust(document);
    //$("iframe", document).each(function(i, e) {
        //font_size_adjust(e.document);
    //});
});

$(document).ready(function($) {
    font_size_adjust(document);
    $("iframe").livequery(function() {
        //font_size_adjust(this.document);
    }).each(function(i, e) {
        //font_size_adjust(e.document);
    });
});*/

(function($) {
    var advancedEditor = {
        theme : "advanced",
        mode : "none",
        plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups,dbfilebrowser",
        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect,|,cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime,preview,|,forecolor,backcolor,|,tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen,|,insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_buttons4 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,
        external_link_list_url : "scripts/linklist.php",
        spellchecker_report_misspellings : true,
        document_base_url : site.url + "/",
        content_css : site.url+"/templates/css/init.css",
        body_class : "column",
        body_id : "body",
        apply_source_formatting: true
    };
    var basicEditor = {
        theme_advanced_buttons1 : "|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull|,sub,sup,|,link,unlink,anchor,image,media,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_buttons4 : ""
    };
    $("textarea[name$='Content']").livequery(function() {
        if(tinyMCE) {
            tinyMCE.init(advancedEditor);
            tinyMCE.execCommand("mceAddControl", true, $(this).attr('id', $(this).attr('name')).attr('id'));
        }
    });
    /*$("textarea[name='piDescription'], textarea[name='pmDescription']").livequery(function() {
        if(tinyMCE) {
            tinyMCE.init(basicEditor);
            tinyMCE.execCommand("mceAddControl", true, $(this).attr('id', $(this).attr('name')).attr('id'));
            fontAdjustTimer = setInterval(font_size_adjust, 250);
        }

    });*/
    $("textarea.mce").livequery(function() {
        if(tinyMCE) {
            switch ($(this).attr('type')){
                case "basic":
                    tinyMCE.init(basicEditor);
                break;
                case "advance":
                    tinyMCE.init(advancedEditor);
                break;
            }
            
            tinyMCE.execCommand("mceAddControl", true, $(this).attr('id', $(this).attr('name')).attr('id'));
            fontAdjustTimer = setInterval(font_size_adjust, 250);
        }

    })
    $("textarea[name$='Comments']").livequery(function() {
        if(tinyMCE) {
            tinyMCE.init(basicEditor);
            tinyMCE.execCommand("mceAddControl", true, $(this).attr('id', $(this).attr('name')).attr('id'));
            fontAdjustTimer = setInterval(font_size_adjust, 250);
        }
    });
})(jQuery);

$(document).ready(function() {
    //kawsar's---------------------------------------------------------------FarsiPraise
    //alert("K");
    
    /*$("a").click(function(){
        $(this).trigger('blur');
    });*/
    
    //kawsar's---------------------------------------------------------------OLD
    /*$('a.show_hide_control').click(function() {
        $('div.show_hide_object#'+$(this).attr('object')).toggle();
        return false;
    });
    
    $('input[name="select_all"]').livequery("click", function() {
        $(this).parents('table').find('input.select_all').attr("checked", this.checked);
    });
    
    $('table.list').not(".tree").dataTable({
        bPaginate: true,
        bLengthChange: false,
        bFilter: false,
        bSort: true,
        bInfo: false,
        bAutoWidth: false
    });*/
    
});

$(document).ready(function($) {
    // hide leaked characters resulting from XHTML DTD extension
    if(document.body.firstChild.data == '] > ')
        document.body.firstChild.data = '';
    
    // nested ordered lists
    $("ol.nested").livequery(function() {
        $(this).nestedDecimalList();
    });
    
    // animate in-page links
    $('a[href*=#]').smoothScroll();
    
    // setup defaults for validator
    $.extend($.validator.messages, {
        required: "*",
        nowhitespace: "No Whitespace",
        number: "Number Only"
    });
    
    // auto validate selected forms
    $("form.validate").validate();
    
    // nested array fields
    $(".group").livequery(reindex = function() {
        $(".group").each(function(i, e) {
            if($(this).hasClass('dummy'))
                return;
            var index = $(this).parent().find(".group:not(.dummy)").index(this);
            if(index < 0) return;
            var group = $(this).attr('group');
            $("input, select, textarea", this).each(function(i, e) {
                var name = $(e).attr('name').replace(new RegExp(group + '\[[0-9]*\]'), group + '[' + index + ']');
                $(e).attr('name', name);
            });
        });
    }, reindex);
    
    // tabs
    $("div.tabs.ajax").livequery(function() {
        $("ul a", this).each(function(i, e) {
            $(e).attr("href", $(e).attr("href") + " .content");
        });
        $("ul", this).tabs("div.panes", {effect: "ajax"});
    });
    $("div.tabs:not(.ajax)").livequery(function() {
        $("ul", this).tabs("div.panes");
    });
    
    // jQuery UI Tabs
    $("ul.tabs").tabs("div.tab_panes > div");
    
    // add css class to buttons
    $("input[type='button'], input[type='submit'], input[type='reset'], button").addClass('button');
    
    // pre-fill article name/heading/url from title
    $("input[name='articleName']").livequery('change', function() {
        var title = $(this).parents().find("input[name='articleTitle']");
        if(!title.val()) title.val($(this).val());
        var heading = $(this).parents().find("input[name='articleHeading']");
        if(!heading.val()) heading.val($(this).val());
        var url = $(this).parents().find("input[name='articleURL']");
        if(!url.val()) url.val($(this).val().toLowerCase().replace(/[^\w]+/, ''));
    });

    // bind date fields with calendar control
    $("input.datetime, input[name$='DateTime']").livequery(function() {
        $(this).datepicker({
            showOn: 'focus',
            dateFormat: 'yy-mm-dd',
            time24h: true,
            changeMonth: true,
            changeYear: true,
            duration: '',
            showTime: true,
            constrainInput: true,
            stepMinutes: 10,
            stepHours: 1
            //showOtherMonths: true
        });
    });
    $("input.date, input[name$='Date']").livequery(function() {
        $(this).datepicker({
            showOn: 'focus',
            dateFormat: 'yy-mm-dd',
            time24h: true,
            changeMonth: true,
            changeYear: true,
            duration: '',
            showTime: false,
            constrainInput: true,
            stepMinutes: 10,
            stepHours: 1
        });
    });

    $("input[name$='State']").livequery(function() {
        var select = $('<select></select>').attr('name', $(this).attr('name')).attr('rawvalue', $(this).attr('value'));
        $(this).replaceWith(select);
        select.trigger('pop');
    });

    // bind/autopopulate State fields
    $("select[name$='State']").livequery('pop', function() {
        if(!(country = $(this).closest("form").find("select[name$='Country']").val()))
            return false;
        var select = $(this);
        $.get(site.url + "/_rpc.html", {c: "Country", a: "listRegionAssoc", k: country}, function(data) {
            if(data.result)
                select.empty();
            $.each(data.result, function() {
                select.append('<option>' + this + '</option>');
            });
            select.val(select.attr('rawvalue'));
        }, "json");
    });

    $("select[name$='Country']").livequery('change', function() {
        $(this).closest("form").find("select[name$='State']").trigger('pop');
    }).trigger('change');

    // autopopulate State/City fields
    $("input[type='text'][name$='Zip']").livequery('change', function() {
        var input = $(this);
        var country = $("input[type='text'][name$='Country'], select[name$='Country']");
        var state = $("input[type='text'][name$='State'], select[name$='State']");
        var city = $("input[type='text'][name$='City'], select[name$='City']");
        $.get(site.url + "/_rpc.html", {c: "Zip", k: country.val() + input.val()}, function(data) {
            if(!data.result) return;
            state.val(data.result.AdminName1);
            city.val(data.result.AdminName3 ? data.result.AdminName2 : data.result.AdminName2 ? data.result.AdminName2 : data.result.AdminName1);
        }, "json");
    });
    
    // Clear selection aid for multiple-select controls
    $("input.clear-select").click(function() {
        $(this).siblings("select").val(null);
    });
    
    $('.dummy').livequery(function() {
        $(this).hide('slow');
        $(this).find('input, select, textarea').attr("disabled", "disabled");
    });
    
    $("fieldset input[value='+']").livequery('click', function() {
        $(".dummy", $(this).parents("fieldset")).clone().removeClass("dummy").appendTo($(this).parents("fieldset")).show().find("input, select, textarea").attr("disabled", "");
    });
    
    $("fieldset input[value='-'], fieldset input[value='x']").livequery('click', function() {
        $(this).parents(".group").remove();
    });
    
    //table - Select All - checkbox
    $('input[name="select_all"]').livequery("click", function() {
        $(this).parents('table').find('input.select_all').attr("checked", this.checked);
    });
    
    // hide messages
    $("#body > div.message,#body > div.message > img.close").livequery('click', function() {
        $(this).fadeOut('slow');
    });
    
    // dataTable
    $('table.dataTable').dataTable({
        bPaginate: true,
        bLengthChange: true,
        bFilter: true,
        bSort: true,
        bInfo: false,
        bAutoWidth: true
    });
});

