﻿var DEBUG = false;
var SHOWCALLSTACK = false;
var KEYhasRated = 'Вы уже оценивали эту запись';
var KEYwebRoot = '/BlogEngine.Web/';
var KEYsavingTheComment = 'Saving the comment...';
var KEYcomments = 'Comments';
var KEYcommentWasSaved = 'Comment was saved';
var KEYcommentWaitingModeration = 'Thank you for the feedback. The comment is now awaiting moderation';
var KEYcancel = 'Cancel';
var KEYfilter = 'Filter';


function Ainstainer() { }
Ainstainer.Control = function(id)
{	
	this.ids = new Array();
	this.ids.push(id);
	this.getElement = function() {
		if (this.ids.length == 0)
			throw 'ids.length = 0';			
		return $Id(this.ids[0]);
	};
	this.addElement = function(id) {
		this.ids.push(id);
	};
	this.getElements = function() {
		var res = new Array();
		for (var id in this.ids) {
			res.push($Id(id))
		}
		return res;
	};
}

Ainstainer.OnLoadRunnerFlag = false;
Ainstainer.OnLoadFunctions = new Array();
Ainstainer.OnLoadRunner = function() 
{    
    for (var i = 0; i < Ainstainer.OnLoadFunctions.length; i++) 
    { 
        Ainstainer.OnLoadFunctions[i]();
    } 
};

Ainstainer.SafeAddOnLoad = function(fn) 
{    
//debugger;
    if (window.onload) {
        if (!Ainstainer.OnLoadRunnerFlag) 
        {
            Ainstainer.OnLoadRunnerFlag = true; 
            Ainstainer.OnLoadFunctions[0] = window.onload;
            window.onload = function() { Ainstainer.OnLoadRunner(); };
        } 
        Ainstainer.OnLoadFunctions[Ainstainer.OnLoadFunctions.length] = fn;
    } else 
    { 
    //window.onload = function(){ alert('onload');fn(); }
        window.onload = fn; 
    }
};

String.prototype.Contains = function(/*string*/str /* , ... */) {
// summary:
//	Returns true if 'str' contains any of the arguments 2 -> n
	for(var i = 1; i < arguments.length; i++) {
		if(str.indexOf(arguments[i]) > -1){
			return true; // boolean
		}
	}
	return false; // boolean
} 



function $Id(id)
{
    var obj = document.getElementById(id);
    if(obj == null){        
        throw new Exception("Element '"+id +"' is null");
    }
    return obj;
   }

   function GetText(control) {
   	if (document.all) {
   		control.innerText;
   	} else {
   		return control.textContent;
   	}

   }

function MoveElement(source,destination){
    var elSource = $Id(source);
    if(!elSource)
        throw new Exception('Source element {'+source+ '} is null');
    var elDestination = $Id(destination);
    if(!elDestination)
        throw new Exception('Destination element {'+destination+ '} is null');
      try{  
    elDestination.innerHTML = elSource.innerHTML;
    elSource.style.display = 'none';
    elSource.innerHTML = '';    
    }catch(e){CatchException(e)}    
}


function Method(caller)
{
    this.body = caller.toString();
    this.arguments = caller.arguments;        
}
Method.prototype.toString = function()
{
    var str = '\nArguments:\n'
    for(var i=0;i< this.arguments.length;i++)
    {
        str+='['+i+'] '+this.arguments[i]+'\n';
    }
    return "Method:\n" + this.body+str;
}

function Exception(message)
{   
    this.Message = Exception.arguments[0];
    if(SHOWCALLSTACK)
        this.Method  = new Method(Exception.caller);
    if(Exception.arguments.length>1)
        this.innerException = Exception.arguments[2];
}

Exception.prototype.toString = function()
{
    if(this.Method)
        return "Exception\n{\n Message: '"+this.Message+"',\n "+this.Method+"\n}";
    return "Message:\n  "+this.Message;
}
function CatchException(e)
{
    if(!DEBUG)
        return;    
    alert(e)
}

function GoTo(url) 
{
    window.open(url);
}
var KEYwebRoot = '/'
function ToggleCommentMenu(element) {
   	element.className = 'selected';   	
   	if (element.id == 'preview') {
   		$Id('compose').className = '';
   		$Id('commentCompose').style.display = 'none';
   		$Id('commentPreview').style.display = 'block';
   		$Id('commentPreview').innerHTML = '<img src="' + KEYwebRoot + 'images/ajax-loader.gif" alt="Loading" />';
   		var argument = $Id('commentPreview').innerHTML;
   		AddComment(true);
   	}
   	else {
   		$Id('preview').className = '';
   		$Id('commentPreview').style.display = 'none';
   		$Id('commentCompose').style.display = 'block';
   	}
}
function EndShowPreview(arg, context) {
   	$Id('commentPreview').innerHTML = arg;
}
function AddComment(preview) {
	try{
	
		var isPreview = preview == true;	
		if (!isPreview) {
			$Id("btnSaveAjax").disabled = true;
			$Id("ajaxLoader").style.display = "inline";
			$Id("status").className = "";
			$Id("status").innerHTML = KEYsavingTheComment;
		}

		var author = nameBox.value;
		var email = emailBox.value;	
		var country = countryDropDown ? countryDropDown.value : "";
		var content = contentBox.value;
		//var notify = $Id("cbNotify").checked;
		//var captcha = captchaField.value;

		var callback = isPreview ? EndShowPreview : AppendComment;
		var argument = author + "-|-" + email + "-|-" + country + "-|-" + content + "-|-";
		// + notify + "-|-" 
		// + "-|-" + captcha;
		argument += isPreview;
		alert(WebForm_DoCallback);
		WebForm_DoCallback(ajaxPageId, argument, callback, 'comment', null, false);
	
	}catch(e){alert(e);}
	if (!isPreview && typeof OnComment != "undefined")
		OnComment(author, email, website, country, content);
}
function SetFlag(iso) {
	if (iso.length > 0)
		flagImage.src = KEYwebRoot + "images/flags/" + iso + ".png";
	else
		flagImage.src = KEYwebRoot + "images/flags/pixel.gif";
}
////prototype js
//var $A = Array.from = function(iterable) {
//  if (!iterable) return [];
//  if (iterable.toArray) {
//    return iterable.toArray();
//  } else {
//    var results = [];
//    for (var i = 0; i < iterable.length; i++)
//      results.push(iterable[i]);
//    return results;
//  }
//}
//Object.extend = function(destination, source) {
//  for (var property in source)
//    destination[property] = source[property];
//  return destination;
//};

//Object.extend(Object, {
//  inspect: function(object) {
//    try {
//      if (Object.isUndefined(object)) return 'undefined';
//      if (object === null) return 'null';
//      return object.inspect ? object.inspect() : String(object);
//    } catch (e) {
//      if (e instanceof RangeError) return '...';
//      throw e;
//    }
//  },

//  toJSON: function(object) {
//    var type = typeof object;
//    switch (type) {
//      case 'undefined':
//      case 'function':
//      case 'unknown': return;
//      case 'boolean': return object.toString();
//    }

//    if (object === null) return 'null';
//    if (object.toJSON) return object.toJSON();
//    if (Object.isElement(object)) return;

//    var results = [];
//    for (var property in object) {
//      var value = Object.toJSON(object[property]);
//      if (!Object.isUndefined(value))
//        results.push(property.toJSON() + ': ' + value);
//    }

//    return '{' + results.join(', ') + '}';
//  },

//  toQueryString: function(object) {
//    return $H(object).toQueryString();
//  },

//  toHTML: function(object) {
//    return object && object.toHTML ? object.toHTML() : String.interpret(object);
//  },

//  keys: function(object) {
//    var keys = [];
//    for (var property in object)
//      keys.push(property);
//    return keys;
//  },

//  values: function(object) {
//    var values = [];
//    for (var property in object)
//      values.push(object[property]);
//    return values;
//  },

//  clone: function(object) {
//    return Object.extend({ }, object);
//  },

//  isElement: function(object) {
//    return !!(object && object.nodeType == 1);
//  },

//  isArray: function(object) {
//    return object != null && typeof object == "object" &&
//      'splice' in object && 'join' in object;
//  },

//  isHash: function(object) {
//    return object instanceof Hash;
//  },

//  isFunction: function(object) {
//    return typeof object == "function";
//  },

//  isString: function(object) {
//    return typeof object == "string";
//  },

//  isNumber: function(object) {
//    return typeof object == "number";
//  },

//  isUndefined: function(object) {
//    return typeof object == "undefined";
//  }
//});

//Object.extend(Function.prototype, {
//  argumentNames: function() {
//    var names = this.toString().match(/^[\s\(]*function[^(]*\(([^\)]*)\)/)[1]
//      .replace(/\s+/g, '').split(',');
//    return names.length == 1 && !names[0] ? [] : names;
//  },

//  bind: function() {
//    if (arguments.length < 2 && Object.isUndefined(arguments[0])) return this;
//    var __method = this, args = $A(arguments), object = args.shift();
//    return function() {
//      return __method.apply(object, args.concat($A(arguments)));
//    }
//  },

//  bindAsEventListener: function() {
//    var __method = this, args = $A(arguments), object = args.shift();
//    return function(event) {
//      return __method.apply(object, [event || window.event].concat(args));
//    }
//  },

//  curry: function() {
//    if (!arguments.length) return this;
//    var __method = this, args = $A(arguments);
//    return function() {
//      return __method.apply(this, args.concat($A(arguments)));
//    }
//  },

//  delay: function() {
//    var __method = this, args = $A(arguments), timeout = args.shift() * 1000;
//    return window.setTimeout(function() {
//      return __method.apply(__method, args);
//    }, timeout);
//  },

//  defer: function() {
//    var args = [0.01].concat($A(arguments));
//    return this.delay.apply(this, args);
//  },

//  wrap: function(wrapper) {
//    var __method = this;
//    return function() {
//      return wrapper.apply(this, [__method.bind(this)].concat($A(arguments)));
//    }
//  },

//  methodize: function() {
//    if (this._methodized) return this._methodized;
//    var __method = this;
//    return this._methodized = function() {
//      return __method.apply(null, [this].concat($A(arguments)));
//    };
//  }
//}); 
///* Based on Alex Arnell's inheritance implementation. */
//var Class = {
//  Create: function() {
//    var parent = null, properties = $A(arguments);
//    if (Object.isFunction(properties[0]))
//      parent = properties.shift();

//    function klass() {
//      this.initialize.apply(this, arguments);
//    }

//    Object.extend(klass, Class.Methods);
//    klass.superclass = parent;
//    klass.subclasses = [];

//    if (parent) {
//      var subclass = function() { };
//      subclass.prototype = parent.prototype;
//      klass.prototype = new subclass;
//      parent.subclasses.push(klass);
//    }

//    for (var i = 0; i < properties.length; i++)
//      klass.addMethods(properties[i]);

//    if (!klass.prototype.initialize)
//      klass.prototype.initialize = Prototype.emptyFunction;

//    klass.prototype.constructor = klass;

//    return klass;
//  }
//};

//Class.Methods = {
//  AddMethods: function(source) {
//    var ancestor   = this.superclass && this.superclass.prototype;
//    var properties = Object.keys(source);

//    if (!Object.keys({ toString: true }).length)
//      properties.push("toString", "valueOf");

//    for (var i = 0, length = properties.length; i < length; i++) {
//      var property = properties[i], value = source[property];
//      if (ancestor && Object.isFunction(value) &&
//          value.argumentNames().first() == "$super") {
//        var method = value;
//        value = (function(m) {
//          return function() { return ancestor[m].apply(this, arguments) };
//        })(property).wrap(method);

//        value.valueOf = method.valueOf.bind(method);
//        value.toString = method.toString.bind(method);
//      }
//      this.prototype[property] = value;
//    }

//    return this;
//  }
//};


