/**
 * Для залогиненных
 */

var JQ = $;
var Common = {}
Function.prototype.bind = function() {  
    if (arguments.length < 1 && typeof arguments[0] != "undefined") return this;  
    var __method = this, args = [];  
    for(var i=0;i<arguments.length;i++){ args.push(arguments[i]);}  
      
    var object = args.shift();  
    return function() {  
      var args_to_apply = []  
        
      for(var i=0;i<args.length;i++){ args_to_apply.push(args[i]);}  
      for(var i=0;i<arguments.length;i++){ args_to_apply.push(arguments[i]);}  
      return __method.apply(object, args_to_apply);  
   }  
};

function empty(s) {
    return /^\s*$/.test(s);
};

function trim(s) {
    if(s)
        return s.replace(/(^\s+)|(\s+$)/g, '');
    return '';
};

function escapeHTML(txt){
	return $("<div/>", {
		text: txt
	}).html();
};


var DefaultValues = {
    init: function() {
        var nodes = document.all || document.getElementsByTagName('*');
        for(var i = 0; i < nodes.length; i++) {
            if(nodes[i].getAttribute('default'))
                DefaultValues.initNode(nodes[i], nodes[i].getAttribute('default'), nodes[i].getAttribute('defaultColor'));
        }
    },
    
    initNode: function(node, txt, color) {
        node = $(node);
        if(empty(node.value))
            node.value = txt;

        node.css("color", (node.value == txt) ? (color || '#666666') : '#000000');

        node.bind('focus',
			function() {
            	if(this.value == txt) {
                	this.style.color = '#000000';
                	this.value = '';
            	}
        	});
        
        node.bind('blur', function() {
            if(empty(this.value)) {
                this.style.color = color || '#666666';
                this.value = txt;
            }
        });
        
        node.DefaultValuesReset = function() {
            this.style.color = color || '#666666';
            this.value = txt;
        }.bind(node);
    }
};

$(DefaultValues.init);

Common.executeJS = function(root){
	var list = root.getElementsByTagName('SCRIPT');
	var hash = {};
	for (var i = 0; i < list.length; i++) 
		hash[i] = list.item(i);
	
	$(function(){
		for (var i in hash) {
			var sc = document.createElement("script");
			sc.id = hash[i].id;
			sc.type = hash[i].type;
			sc.text = hash[i].text;
			this.appendChild(sc);
		}
	});
};

Common.fillTemplate = function(node, params){
	node = $(node);
	var str = node.html();
	for(var p in params)
        str = str.replace(new RegExp(p, 'g'), params[p]);
   	return str;
};

function stopEvent(e) {
    if(!e) e = event;
    if(typeof(e.stopPropagation) == 'function') e.stopPropagation();
    if(e.preventDefault) e.preventDefault();
    if(typeof(e.cancelBubble) == 'boolean') e.cancelBubble = true;
    e.returnValue = false;
    return false;
};

Common.askForAction = function(title, question, action){
	var d = $("<div/>", {
			html: question,
			className: "warn-popup"
		});
		d.dialog({
			modal: true,
			title: title,
			buttons: {
				"Нет": function(){
					d.dialog("close");
				},
				"Да": function(){
					action();
					d.dialog("close");
				}
			}
		});
};

Common.removeFriend = function(url){
	Common.askForAction("Удаление друга", "Вы действительно хотите удалить друга?", function(){
		window.location.href = url;
	});
};