﻿$.ajaxSetup({cache:false});
function showProps(o) {var s = '';for (var i in o) {s += i + ': ' + o[i] + '\r\n'; } alert(s);  }

function getUrl(controller, action, arg1, arg2) {
    var url='/'+controller;
    if (action) url+='/'+action;
    if (arg1) url+='/'+arg1;
    if (arg2) url+='/'+arg2;
    return url;
}

function setURLParam(url, param, value) {
    var s = url;
    var remChar='';
    if (s.indexOf('&'+param+'=')!=-1) {remChar='&'}
    if (s.indexOf('?'+param+'=')!=-1) {remChar='?'}
        
    if (remChar!='') {
        var i=s.indexOf(remChar+param+'=');
        var i2=s.indexOf('&',i+1);
        s = s.substring(0,i);
        if (i2!=-1) s += url.substring(i2, url.length);
    }
    if (s[s.length-1]=='#') s=s.substring(0,s.length - 1);    
    return s+'&'+param+'='+value; 
}

function getUrlResponse(url, data)
{
    return $.ajax({ type: 'POST', url: url, async: false, data: data }).responseText;
}

function LinkFormatter(cellvalue, options, rowObject)
{
    return '<a href=' + options.colModel.formatoptions.href + '/' + escape(cellvalue) + ">" + cellvalue + "</a>";
}

window.UIInitFunctions = [uiInitCommon];
window.InitUI = function() { for (var i=0;i<UIInitFunctions.length;i++) UIInitFunctions[i](); }

function uiInitCommon(){
    $([2,3,4,5,10]).each(function(){$('.corner'+this).corner(''+this+'px').removeClass('corner'+this)});
    $([2,3,4,5,10]).each(function(){
        var n=this;
        $('.cornerB'+n).each(function(){
            var el=$(this);
            var clr = el.css('border-color');
            el.wrap('<div style="background-color:'+clr+'"></div>');
            el.corner('round 3px').parent().css('padding', '1px').corner('round 5px');
            el.removeClass('cornerB'+n);
        });

    });
    if ($.fn.datepicker) {
        $('.citdate').removeClass('citdate').datepicker({ dateFormat: 'yy/mm/dd', changeMonth: true, changeYear: true, yearRange: '-5:+5' });
    }
}

$(function() {
    InitUI();
    $().ajaxStart(function() { });
    $().ajaxStop(function() { InitUI(); });
//    $.ajaxSetup({error: function(xhr, ajaxOptions, thrownError)
//                    {
//                        alert('Sorry, an error occurred while processing your request.');
//                    }});
    
    // Load tabs
    $('.tabs').removeClass('tabs').tabs();
    
    // Load JQGrid default configuration
    if ($.jgrid) {
        $.extend($.jgrid.defaults,
        {
            mtype: 'POST',
            datatype: 'json',
            rowNum: 50,
            rowList: '',
            height: '100%',
            cellLayout: 9,
            viewrecords: true,
            hidegrid: false,
            autowidth: true
        });
    }
});

window.confirmModal=function(title,msg,yesCB,noCB) {
    $('#dlgContents').html(msg);
    $('#dlg').dialog({modal:true,
        title: title,
        buttons: {
            No: function() {
		        if (noCB) noCB();
			    $(this).dialog('close');
		    },
		    Yes: function() {
		        if (yesCB) yesCB();
			    $(this).dialog('close');
		    }
	    }
    });
};

window.showPopup=function(title,html,btns,opts) {
    if (html != null) $('#dlgContents').html(html);
    opts = $.extend({modal:true,title: title,buttons: btns}, opts||{} );
    $('#dlg').dialog(opts);
};

window.showPopupRemote=function(title,url,btns,opts) {
    $.get(url, function(html) {
        showPopup(title,html,btns,opts);
    });
};

window.sendLightbox=function(id) {
    //$('#dlg').dialog({ width: 500 });
    showPopupRemote('Share your lightbox','/Lightbox/Send/'+id,
        {
            Cancel: function() {$(this).dialog('close');},
            Send: function() {
                var dlg=$(this);
                $.post('/Lightbox/Send/'+id,{from:$('#lb_from').val(),to:$('#lb_to').val(),message:$('#lb_msg').val()}, 
                    function(res){
                        if (res=='1') dlg.dialog('close');
                        else notify('Unable to send the lightbox');
                    }
                );
            }
        },
        {width:550}
    );
};

window.notify=function(msg,closeCB) {
    $('#dlgContents').html(msg);
    $('#dlg').dialog({modal:true,
        title: 'Notification',
        buttons: {
            Ok: function() {
                if (closeCB) closeCB();
		        $(this).dialog('close');
		    }
	    }
    });
};

window.downloadAsset=function(id) {
    var fn=$('#thFN'+id).val();
    var url="/Asset/Original/"+id+'/'+fn;
    //don't ask for resize if PDF
    if (fn.indexOf('.pdf')!=-1) {
        window.location = url;
        return;
    }

    //ask for resizing
    $('#dlgContents').html('<span style="color:#444444; font-family:Helvetica,Verdana,Geneva,Arial,sans-serif; font-size:8pt;">Specifiy the <b>height</b> (for portrait images) or the <b>width</b> (for landscape images) to resize download to:<br/><br/>' +
        '<input type="text" style="width:40px" id="imgDownloadDim" /> pixels<br/><br/>(Leave blank for original size)</div>');
    $('#dlg').dialog({ modal: true,
        title: '<span style="font-family:Helvetica,Verdana,Geneva,Arial,sans-serif; font-size:8pt;">Download item</span>',
        resizable: false,
        buttons: {
            Cancel: function(){$(this).dialog('close')},
            Ok: function() {
            
                window.location = url+"?maxdim="+$('#imgDownloadDim').val();
            
		        $(this).dialog('close');
		    }
	    }
    });
    setTimeout(function() { $('#imgDownloadDim').focus() }, 500);
};

window.adminAsset = function(id) {
    showPopupRemote('Edit Asset Details', '/Asset/Admin/' + id,
        {
            Cancel: function() { $(this).dialog('close'); },
            Save: function() {
                var dlg = $(this);
                $("#frmAsset").ajaxSubmit({
                    success: function() { dlg.dialog('close'); }
                });
            }
        },
        { width: 550 }
    );
};