if(! ("console" in window) ){
  console = {
    log: function(){},
    dir: function(){}
  };
}else{

  if(!console.log){console.log = function(){};}
  if(!console.dir){console.dir = function(){};}
}
var flashObj = null;
function setPhotoParams(params){
	//console.log("setPhotoParams");
	//console.dir(params.evalJSON());
	if(Object.isString(params)){
		var hash = eval('('+params+')');
		if(hash){
			var html = $H(hash).inject("", function(acc, item){return acc+'<input type="hidden" name="'+item.key+'" value="'+item.value+'">'; }.bind(this));
		}
		if(html && html.length){
			$("paramsContainer").innerHTML = html;
		}
		if(hash.groupID && hash.templID){
			$("content_center").select("a.href_right").each(function(link){link.href = "/fx-new/"+hash.groupID+"/"+hash.templID;})
		}
	}
	
}
function setState(height, className){
	if(Object.isString(className)){
		$("container").className = className;
	}
	var flashObj = $("fx_flash");
	if(flashObj && height){
		flashObj.setStyle({height: height+"px"});
	}
	var mainMenu = $("main_menu");
	var curItem = null;
	var changeMenuItem = function(event){
		if($("container").className == "correction"){return;}
		if(Object.isString(this.id)){
			try{
				if(!curItem){curItem = mainMenu.down("li.active");}
				if(curItem){curItem.removeClassName("active");}
				curItem = $(this).addClassName("active");
				flashObj.setTemplate(this.id);
				event.stop();
				reloadBannersAndCounter();
			}catch(e){};
		}
	}
	if(mainMenu){
		mainMenu.childElements().invoke("observe", "click", changeMenuItem);
	}
}

function enableCam(){
	var webCamBlock = $("webCamBlock");
	
	if(webCamBlock){
		webCamBlock.style.display = "block";
	}
	
	if (Cookies.is_cam_checked == 'undefined' || !Cookies.is_cam_checked)
	{
        new Ajax.Request('/check-cam',
        {
            method:'post',
            parameters: {'cam_exist': 1},
            onSuccess: function(transport){},
            onFailure: function(){  }
        });
	
	}
}
function addFriend(link){
  var table = $(link).up("div").previous("table");
  if(table.down("tbody")){table = table.down("tbody");}
  var lastRow = table.select("tr").last();
  var rowCopy = lastRow.cloneNode(true);
  table.appendChild(rowCopy);
  rowCopy.descendants().each(function(element){
    if(element.tagName.toLowerCase() == "input"){
      element.value = '';
      ["id", "name"].each(function(attr){
        var attrValue = element.getAttribute(attr);
        if(attrValue){
          element.setAttribute(attr, increaseAttr(attrValue));
        }
      });
    }
    if(element.tagName.toLowerCase() == "label"){
      var attrValue = element.htmlFor;
      if(attrValue){
        element.htmlFor = increaseAttr(attrValue);
      }
    }
  });
}
function increaseAttr(attrValue){
  var digit = attrValue.match(/\d+/);
  if(digit && digit.length){
    var number = digit[0];
    return attrValue.replace(number, parseInt(number)+1);
  }
  return attrValue;
}
function copyEffect(index){
	var highlightDiv = $(index).up("div.code");
  if(highlightDiv.CopyingEffectStatus){return;}
  highlightDiv.CopyingEffectStatus = true;
  new Effect.Morph(highlightDiv, {
    style: 'background-color:#ffc137;',
		duration: 0.15,
    afterFinish: function(){
			new Effect.Morph(highlightDiv, {
        style: 'background-color:#FFFFFF',
				duration: 0.4,
        delay: 0.5,
        afterFinish: function(){highlightDiv.CopyingEffectStatus = false;}
      });
    }
  });
}
function checkFriends(form){
  var fields = $(form).select("input[name^=email_fr]");
  var partition = fields.partition(function(field){return field.value.blank();});
  if(partition[0].length == fields.length){
    try{new Effect.Highlight(fields[0], {queue: {position:'end', scope: "scope1", limit:1}, duration: 0.4, startcolor: "#EB26A3"});} catch(e){}
    return false;
  }else{
    var incorrectFormat = partition[1].select(function(item){
      var regex = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
      return !regex.test(item.value);
    });
    if(incorrectFormat && incorrectFormat.length){
      incorrectFormat.each(function(field){
        try{new Effect.Highlight(field, {queue: {position:'end', scope: "scope"+field.id, limit:1}, duration: 0.4, startcolor: "#EB26A3"});} catch(e){}
      });
      return false;
    }
  }
  return true;
}
function checkComment(){
  var emptyFields = $("commentatorName", "commentText").select(function(field){ return field.value.blank(); });
  if(emptyFields && emptyFields.length){
    emptyFields.each(function(field){
      try{new Effect.Highlight(field, {queue: {position:'end', scope: "scope"+field.id, limit:1}, duration: 0.4, startcolor: "#EB26A3"});} catch(e){}
    });
    //try {new Effect.Shake(emptyFields[0].up("fieldset"));} catch(e){};
    return false;
  }
  return true;
}
function reloadBannersAndCounter(){
	if($("adsIframeTop")){$("adsIframeTop").src = "/get-banner/top/full-html"+(new Date()).getTime()}
	if($("adsIframeRight")){$("adsIframeRight").src = "/get-banner/right/full-html"+(new Date()).getTime()}
	if($("adsIframeBottom")){$("adsIframeBottom").src = "/get-banner/bottom/full-html"+(new Date()).getTime()}
	if($("adsIframeCnStat")){$("adsIframeCnStat").src = "/get-banner/cnstat/full-html"+(new Date()).getTime()}
}
var webkitCounter = 0;
document.observe("dom:loaded", function(){
	if(Prototype.Browser.WebKit && $("adjustForm")){
		var adjustForm = $("adjustForm");
		adjustForm.observe("submit", function(event){
			adjustForm.action = webkitCounter%2 ? "/process" : "/processing";
			webkitCounter++;
		});
	}
});
