if(!("console" in window)){console = {log: function(){}};}else{if(!console.log){console.log = function(){};}}

// code from jquery.effects.core.js 
$.effects = {};

/******************************************************************************/
/****************************** COLOR ANIMATIONS ******************************/
/******************************************************************************/

// override the animation for color styles
$.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor',
    'borderRightColor', 'borderTopColor', 'borderColor', 'color', 'outlineColor'],
function(i, attr) {
    $.fx.step[attr] = function(fx) {
        if (!fx.colorInit) {
            fx.start = getColor(fx.elem, attr);
            fx.end = getRGB(fx.end);
            fx.colorInit = true;
        }

        fx.elem.style[attr] = 'rgb(' +
            Math.max(Math.min(parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0], 10), 255), 0) + ',' +
            Math.max(Math.min(parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1], 10), 255), 0) + ',' +
            Math.max(Math.min(parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2], 10), 255), 0) + ')';
    };
});

// Color Conversion functions from highlightFade
// By Blair Mitchelmore
// http://jquery.offput.ca/highlightFade/

// Parse strings looking for color tuples [255,255,255]
function getRGB(color) {
        var result;

        // Check if we're already dealing with an array of colors
        if ( color && color.constructor == Array && color.length == 3 )
                return color;

        // Look for rgb(num,num,num)
        if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
                return [parseInt(result[1],10), parseInt(result[2],10), parseInt(result[3],10)];

        // Look for rgb(num%,num%,num%)
        if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
                return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

        // Look for #a0b1c2
        if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
                return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

        // Look for #fff
        if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
                return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

        // Look for rgba(0, 0, 0, 0) == transparent in Safari 3
        if (result = /rgba\(0, 0, 0, 0\)/.exec(color))
                return colors['transparent'];

        // Otherwise, we're most likely dealing with a named color
        return colors[$.trim(color).toLowerCase()];
}

function getColor(elem, attr) {
        var color;

        do {
                color = $.curCSS(elem, attr);

                // Keep going until we find an element that has color, or we hit the body
                if ( color != '' && color != 'transparent' || $.nodeName(elem, "body") )
                        break;

                attr = "backgroundColor";
        } while ( elem = elem.parentNode );

        return getRGB(color);
};

// Some named colors to work with
// From Interface by Stefan Petre
// http://interface.eyecon.ro/

var colors = {
    aqua:[0,255,255],
    azure:[240,255,255],
    beige:[245,245,220],
    black:[0,0,0],
    blue:[0,0,255],
    brown:[165,42,42],
    cyan:[0,255,255],
    darkblue:[0,0,139],
    darkcyan:[0,139,139],
    darkgrey:[169,169,169],
    darkgreen:[0,100,0],
    darkkhaki:[189,183,107],
    darkmagenta:[139,0,139],
    darkolivegreen:[85,107,47],
    darkorange:[255,140,0],
    darkorchid:[153,50,204],
    darkred:[139,0,0],
    darksalmon:[233,150,122],
    darkviolet:[148,0,211],
    fuchsia:[255,0,255],
    gold:[255,215,0],
    green:[0,128,0],
    indigo:[75,0,130],
    khaki:[240,230,140],
    lightblue:[173,216,230],
    lightcyan:[224,255,255],
    lightgreen:[144,238,144],
    lightgrey:[211,211,211],
    lightpink:[255,182,193],
    lightyellow:[255,255,224],
    lime:[0,255,0],
    magenta:[255,0,255],
    maroon:[128,0,0],
    navy:[0,0,128],
    olive:[128,128,0],
    orange:[255,165,0],
    pink:[255,192,203],
    purple:[128,0,128],
    violet:[128,0,128],
    red:[255,0,0],
    silver:[192,192,192],
    white:[255,255,255],
    yellow:[255,255,0],
    transparent: [255,255,255]
};
// end of code from jquery.effects.core.js

// xfade2

var d=document, imgs = new Array(), bullets = new Array(), zInterval = 5000, current=0, next=1, pause=false, timer1;

function so_init() {
    if (!d.getElementById || !d.createElement) return;

    imgs = d.getElementById("imageContainer").getElementsByTagName("img");
    bullets = d.getElementById("bulletsContainer").getElementsByTagName("a");
    for(i=0;i<imgs.length;i++) {
        imgs[i].xOpacity = 0;
        imgs[i].id = i;
        imgs[i].onclick = function() {
            location.href=bullets[this.id].href;
        };
    }
    showImg(1);
    
    for (i=0; i<bullets.length; i++) {
        bullets[i].id = i;
        bullets[i].onmouseover = function() {
            showImg(this.id);
            clearTimeout(timer1);
            current=this.id;
            timer1 = setTimeout("nextImg("+this.id+ ")",zInterval);
        };
    }
    timer1 = setTimeout(nextImg,zInterval);
}
function getNext(c) {
    return imgs[parseInt(c,10)+1]?(parseInt(c,10)+1):0;
}
function animate(obj) {
    obj.xOpacity = obj.xOpacity + (obj.tOpacity - obj.xOpacity)*0.3;
    if (Math.abs(obj.xOpacity-obj.tOpacity)<0.01) {
        obj.xOpacity = obj.tOpacity;
    } else {
        setTimeout("animate(imgs["+obj.id+"]);",50);
    }
    if (obj.xOpacity > 0) {
        obj.style.display = "block";
    } else {
        obj.style.display = "none";
    }
    setOpacity(obj);
}

function showImg(c) {
    current = c;
    for (i = 0; i < imgs.length; i++) {
        imgs[i].tOpacity = (i == c)?.99:0;
        bullets[i].className = (i == c)?"active":"non-active";
        setTimeout("animate(imgs["+i+"]);",50);
    }
}
function nextImg(ca) {
    current = getNext(current);
    showImg(current);
    timer1 = setTimeout(nextImg,zInterval);
}
function setOpacity(obj) {
    if(obj.xOpacity > .99) {
        obj.xOpacity = .99;
        return;
    }
    obj.style.opacity = obj.xOpacity;
    obj.style.MozOpacity = obj.xOpacity;
    obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
}

// end of xfade2

Function.prototype.protoBind = function(context) {
  if (arguments.length < 2 && typeof(arguments[0]) == "undefined" ) return this;
  var __method = this, args = $.makeArray(arguments).slice(1);
  return function() {
    var a = args.concat($.makeArray(arguments));
    return __method.apply(context, a);
  }
};

var Menu = function(container){
    this.container = $(container);
    this.topLevelItems = this.container.children("li"); 
    this.topShowDelay = 1;
    this.showDelay = 1;
    this.hideDelay = 400;
    this.topLevelItems.each($.proxy(this.setEvents, this));
    if(window.opera){
        $(window).bind("unload", $.proxy(this.hideAll, this));
    }else{
        $(window).bind("beforeunload", $.proxy(this.hideAll, this));
    }
    this.showTimer = {};
    this.hideTimer = {};
    this.shown = {};
}
Menu.prototype = {
    setEvents: function(index, element){
        var topLi = $(element);
        topLi.find(">a").click($.proxy(this.topItemsClickHandler, this)); //.mouseenter($.proxy(this.topItemsMouseenterHandler, this)).mouseleave($.proxy(this.mouseleaveHandler, this));   
        topLi.mouseover($.proxy(this.mouseenterHandler, this)).mouseout($.proxy(this.mouseleaveHandler, this));
    },
    topItemsClickHandler: function(event){
        event.preventDefault();
        var item = $(event.target).closest("li");
        this.setDelayShow(item, this.topShowDelay);
    },
    mouseenterHandler: function(event){
			var item = $(event.target).closest("li");
      if($.inArray(item.get(0), this.topLevelItems) == -1){
          this.setDelayShow(item, this.showDelay);
      }else{
        var id = item.attr("id");
        if(!id){return;}
        var itemId = id.replace("menu_", "");
        if(this.hideTimer[itemId]){
            clearTimeout(this.hideTimer[itemId]);
            this.hideTimer[itemId] = null;
        }
			}
    },
    mouseleaveHandler: function(event){
        var item = $(event.target).closest("li");
        this.setDelayHide(item, this.hideDelay);
    },
    setDelayShow: function(item, time){
        var id = item.attr("id");
        var parts = id.split("_");
        var showing = [];
        for(var i = 1; i < parts.length; i++){
            var itemId = parts.slice(1,i+1).join("_");
            this.delayShow(itemId, time);
            showing.push(itemId);
        }
        for(var id in this.hideTimer){
            if($.inArray(id, showing) == -1){
                clearTimeout(this.hideTimer[id]);
                this.hide(id);
            }
        }
    },
    setDelayHide: function(item, time){
        var id = item.attr("id");
        var parts = id.split("_");
        for(var i = 1; i < parts.length; i++){
            var itemId = parts.slice(1,i+1).join("_");
            this.delayHide(itemId, time);
        }
    },
    delayShow: function(itemId, time){
        if(this.hideTimer[itemId]){
            clearTimeout(this.hideTimer[itemId]);
            this.hideTimer[itemId] = null;
        }
        this.showTimer[itemId] = setTimeout(this.checkShow.protoBind(this, itemId), time);
    },
    checkShow: function(itemId){
        if(!this.shown[itemId] && this.showTimer[itemId]){ this.show(itemId); }
    },
    show: function(itemId){
        this.shown[itemId] = true;
        this.showTimer[itemId] = null;
        $("#menu_"+itemId).addClass("over").css({zIndex: 200});
        //$("#menu"+itemId).fadeIn(200);
        $("#menu"+itemId).show().css({zIndex: 200});
    },
    delayHide: function(itemId, time){
        if(this.showTimer[itemId]){
            clearTimeout(this.showTimer[itemId]);
            this.showTimer[itemId] = null;
        }
        this.hideTimer[itemId] = setTimeout(this.checkHide.protoBind(this, itemId), time);
    },
    checkHide: function(itemId){
        if(this.shown[itemId] && this.hideTimer[itemId]){this.hide(itemId);}
    },
    hide: function(itemId){
        this.shown[itemId] = false;
        this.hideTimer[itemId] = null;
        //$("#menu"+itemId).fadeOut(100, function(){$("#menu_"+itemId).removeClass("over");});
        $("#menu"+itemId).css({zIndex: 110}).hide();
        $("#menu_"+itemId).removeClass("over").css({zIndex: 110});
    },
    hideAll: function(){
        for(var id in this.shown){
            this.hide(id);
        }
    }
};

function selectItem( row, href ){
    //  window.open('http://server.iad.liveperson.net/hc/57088204/?cmd=file&file=visitorWantsToChat&site=57088204&referrer='+document.location,'chat57088204','width=472,height=320');
    window.open('https://siteheart.com/webconsultation/4183','siteheart_sitewindow_4183','width=550,height=400,top=30,left=30,resizable=yes');
}

function showForm(obj,val) {
	val > 0 ? $("#"+obj).show().find("input[name=UserName]").focus() : $("#"+obj).hide();
}
function checkPrivat24Form(form){
	/*
	var noErrors = true, firstErrorElem = null;
	$.each(["UserName", "UserPass"], function(index, name){
		var input = $(form).find("input[name="+name+"]");
		if(!$.trim(input.val())){
			noErrors = false;
			input.stop(true, true).animate({backgroundColor: "#86fe1c"}, 300).animate({backgroundColor: "#FFFFFF"}, 300);
			if(!firstErrorElem){firstErrorElem=input;}
		}
	});
	if(firstErrorElem){firstErrorElem.focus();}
	return noErrors;
	*/
	var jForm = $(form);
	var loginField = jForm.find("input[name=UserName]");
	var pwdField = jForm.find("input[name=UserPass]");
	var loginValid = $(form).closest("div").attr("id") == "login_private" ? checkFieldIsPhone(loginField) : checkFieldNotEmpty(loginField);
	var pwdValid = checkFieldNotEmpty(pwdField);
	var firstErrorElem = !loginValid ? loginField : (!pwdValid ? pwdField : null);
	if(firstErrorElem){firstErrorElem.focus(); return false;}
	return true;
}
function checkFieldNotEmpty(field){
	var field = $(field);
	if(field && !$.trim(field.val())){
        showFieldError(field);
		return false;
	}
	return true;
}
function checkFieldIsPhone(field){
	var field = $(field);
	if(!field || field && field.val() && $.trim(field.val()).length == 13 && $.trim(field.val()).indexOf("+380") == 0){return true;}
    showFieldError(field);
    return false;
}
function showFieldError(field){
	$(field).stop(true, true).animate({backgroundColor: "#86fe1c"}, 300).animate({backgroundColor: "#FFFFFF"}, 300);
}
function draw_menu(){
    topMenuItems = $("#menu-top").children();
	topMenuItems.each(draw_one_menu);
}
function draw_one_menu(index, item){
	var menuIndex = index+1;
	var menuConfig = window["menu_"+menuIndex];
	var menuHTML = '<ul id="menu'+menuIndex+'" style="display:none;">';
	$.each(menuConfig, function(itemIndex, menuItemConfig){
		itemIndex++;
		var cssClass = itemIndex == 1 ? ' class="first"' : (itemIndex == menuConfig.length ? ' class="last"' : ''); 
		menuHTML += '<li id="menu_'+menuIndex+'_'+itemIndex+'"'+cssClass+'>'
		  +'<a id="a_menu_'+menuIndex+'_'+itemIndex+'" href="'+menuItemConfig[0]+'">'+menuItemConfig[1]+'</a>';
		
		if(menuItemConfig[2].length){
            var cssClass = menuIndex == topMenuItems.length ? ' class="end"' : '';
			menuHTML += '<ul id="menu'+menuIndex+'_'+itemIndex+'" style="display:none;"'+cssClass+'>';
			$.each(menuItemConfig[2], function(subItemIndex, subMenuItemConfig){
		        subItemIndex++;
				var cssClass = subItemIndex == 1 ? ' class="first"' : (subItemIndex == menuItemConfig[2].length ? ' class="last"' : ''); 
		        menuHTML += '<li id="menu_'+menuIndex+'_'+itemIndex+'_'+subItemIndex+'"'+cssClass+'>'
		          +'<a href="'+subMenuItemConfig[0]+'">'+subMenuItemConfig[1]+'</a></li>';
			});
			menuHTML += '</ul>';
		}
		menuHTML += '</li>';
	});
    menuHTML += '</ul>';
	item.innerHTML += menuHTML;
}

$.getScript("https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js");

