/**
 * displays the appropriate logo for the corresponding paper
 *
 * @param paperPrefix the prefix of the paper
 * @param hostPath the host of the path
 */
function displayPaperLogo(paperPrefix, hostPath){
    var img;
    var path;
    //alert(hostPath);
    if(paperPrefix == 'ek'){
        img = 'ek-logo.jpg';
        path = hostPath;
    }else if(paperPrefix == 'ekn'){
        img = 'ek-logo.jpg';
        path = hostPath+'index.php?lang=np';
    }else if(paperPrefix == 'tkp'){
        path = hostPath+'tkp';
        img = 'logo-ktm-post.gif';
    }else if(paperPrefix == 'kan'){
        path = hostPath+'kantipur';
        img = 'logo-kantipur.gif';
    }else if(paperPrefix == 'kq'){
        path = hostPath+'qatar';
        img = 'logo-kantipur-qatar.gif';
    }else if (paperPrefix == 'nep'){
        path = hostPath+'nepal';
        img = 'logo-nepal.gif';
    }else if (paperPrefix == 'sap'){
        path = hostPath+'saptahik';
        img = 'logo-saptahik.gif';
    }else if (paperPrefix == 'nar'){
        path = hostPath+'saptahik';
        img = 'logo-naari.gif';
    }
    
    $('#paper').html('<a href="'+path+'"><img src ="../../images/'+img+'" /></a>');

    return false;
}

function getTodayDate(){
    var d=new Date();
    var today = d.getFullYear() + '-'+ (d.getMonth() + 1) + '-' +d .getDate();
    return today;
}

function alterFontSize(target, factor){
    var oldFontSize = parseInt(target.css('font-size'));
    //alert(oldFontSize);
    if(factor < 0 && oldFontSize > 13 && oldFontSize <= 20){
        target.css('font-size', (oldFontSize + factor)+'px');
    }else if(factor > 0 && oldFontSize >= 13 && oldFontSize < 20){
        target.css('font-size', (oldFontSize + factor)+'px');
    }
}

function alterFontSize1(target, factor){
    var oldFontSize = parseInt(target.css('font-size'));
    if(factor < 0 && oldFontSize > 14 && oldFontSize <= 20){ //decrease font
        target.css('font-size', (oldFontSize + factor)+'px');
    }else if(factor > 0 && oldFontSize >= 14 && oldFontSize <= 18){ // inccrease font
        target.css('font-size', (oldFontSize + factor)+'px');
    }
}


/**
 * Blueprint for select/combo that requies chaining reload
 *
 * @param <string> src the id of the parent select/combo
 * @targer <string> the id of the child/target select/combo
 * @task <string> the task that is to be handled in the server side
 * @isLeaf <boolean> the value that specifies whether the parent needs to
 *                          trigger the reload of child
 * @child <Combo> the child Combo object
 * @name <string> [optional] the name that needs to be prepended
 */
function Combo(src, target, task, isLeaf, child, name){
    this.src = src;
    this.target = target;
    this.task = task;
    this.isLeaf = isLeaf;

    this.toString = function(){
        return name;
    }

    /**
     * return the curretn selected value of source select/combo
     */
    this.getValue = function(){
        return $('#'+src).val();
    }

    /**
	 * the function that triggers reload
     *
	 * @param url the URL from where the JSON is to be fetched
	 * @param prependSelect if '---SELECT XXX ---' is to be prepended to the options
	 */
    this.reloadChild = function(url, prependSelect, prependVal){
        // the url which queries the database
        var _url = url;

        // the params to be sent as POST varaiables the above URL
        var params ={
            // the task to be performed
            task: this.task,
            // id of the parent select/combo
            parent_id: this.getValue()
        };

        // make the AJAX call the above URL
        $.ajax({
            url: _url,
            type: 'POST',
            data: params,
            dataType: 'json',
            success: function(itemList){
                $('#'+target).empty();
                if(itemList.length > 0){
                    for(var n = 0; n < itemList.length; n++) {
                        $('#'+target).get(0).add(
                            new Option(itemList[n].name,itemList[n].id),
                            document.all ? 0 : null);
                    }
                }
                // if --Select XXX-- needs to prepended to the child select/combo
                if(prependSelect != null || prependSelect == true || prependSelect != ''){
                    //$('#'+target).prepend('<option selected value="">--Select '+child.toString()+'--</select>')
                    if(prependVal != null){
                        $('<option value="">--- '+child.toString()+ ' '+prependVal+'--</option>').prependTo($('#'+target+':first-child'));
                    }else {
                        $('<option value="">--- Select '+child.toString()+'--</option>').prependTo($('#'+target+':first-child'));
                    }
                    $('#'+target+' option[value=""]').attr('selected', 'selected');
                }
            },
            complete:function(){
                // the recursive handler which checks if there is any child element to reload
                if(!child.isLeaf){
                    child.reloadChild(url, prependSelect);
                }
            }
        });
        return false;
    }
}

function tArea(classUsed, toolBar){
    this.classUsed = classUsed;
    this.elements = new Array();
    this.tAInstance = new Array();
    
    if(toolBar==null){
        this.toolBar = "Normal";
    }else{
        this.toolBar = toolBar;
    }
    //alert(this.toolBar);

    this.initTArea = function(){
        this.cleanUP();
        var newArr = new Array();
        $(this.classUsed).each(function(i,elem){
            newArr.push(elem.id);
        });
        this.elements = newArr;
    //alert(this.elements+'='+this.elements.length);
    }

    this.loadTArea = function(width,height){
        if(isNull(width)){
            width = '500px';
        }
        if(isNull(height)){
            height = '450px';
        }
        var tAreaTmp = new Array();
        var myBar = this.toolBar;

        $(this.elements).each(function(i, elem){
            tAreaTmp.push(CKEDITOR.replace(elem, {
                toolbar: myBar,
                width: width,
                height: height
            }));
        });

        this.tAInstance = tAreaTmp;

    }

    this.cleanUP = function(){
        $.each(this.tAInstance, function(i,item){
            if(item)
                CKEDITOR.remove(item);
        });
    }

    this.clearData = function(){
        $.each(this.tAInstance, function(i,item){
            if(item)
                item.setData('');
        });
    }

    this.saveReady = function(){
        var dataArr = this.elements;
        $(this.tAInstance).each(function(i, elem){
            $("#"+dataArr[i]).val(elem.getData());
        });
    }

}

$.fn.disable = function() {
    return this.each(function() {
        if (typeof this.disabled != "undefined") this.disabled = true;
    });
}

$.fn.enable = function() {
    return this.each(function() {
        if (typeof this.disabled != "undefined") this.disabled = false;
    });
}

function slideEdNews(target, prev, next){
    target = '#' + target;
    $(target+' ul').cycle({
        speed: 200,
        timeout: 0,
        pause: 1,
        prev: target+' .'+prev,
        next: target+' .'+next
    });
}

function reloadCaptcha(url){
    $('#captcha').attr('src', url +'captcha/securimage_show.php?sid='+ Math.random());
    return false;
}