Global = {
	mouseY: null,
	mouseY: null,
	initialize : function()
    {
        var ua = navigator.userAgent.toLowerCase();

        this.isOpera = (ua.indexOf('opera') > -1);
        this.isSafari = (ua.indexOf('safari') > -1);
        this.isGecko = (!this.isOpera && !this.isSafari && ua.indexOf('gecko') > -1);
        this.isIE = (!this.isOpera && ua.indexOf('msie') > -1);
        this.isMac = (ua.indexOf('macintosh') > -1);

        if(this.isIE)
        {
            var msieIndex = ua.indexOf('msie');
            this.IEVersion = parseFloat(ua.substring(msieIndex + 5, ua.indexOf(';', msieIndex)));
        }
        else
        {
            this.IEVersion = -1;
        }

        if(this.isGecko) {
            var index = ua.indexOf('firefox');
            this.FFVersion = parseFloat(ua.substring(index + 8 ));
        }

        this.setMoveListener();
    },

    setMoveListener: function(){
    	if(Global.isIE){
            document.attachEvent("onmousemove", Global.mouseMove);
        }else{
            document.addEventListener('mousemove',Global.mouseMove,true);
        }
    },

    mouseMove: function (e){
    	try{
            Global.mouseX = (!Global.isIE) ? e.pageX : event.x+document.body.scrollLeft;
            Global.mouseY = (!Global.isIE) ? e.pageY : event.y+document.body.scrollTop;
        }catch(e){}
    }
}

Global.initialize();

Array.prototype.in_array = function(value){
    for(var k=0;k<this.length;k++){
        if(value == this[k]){
            return true;
        }
    }
    return false;
}

Array.prototype.array_unique = function(){
    var newArray = new Array();

    for (var i=0; i<this.length; i++) {
        if(newArray.in_array(this[i])){
            continue;
        }
        newArray.push(this[i]);
    }

    return newArray;
}

function addProductToCart(form_id,tab_id){

	openBinPopup(tab_id);
	addToBin(form_id);

}

/* functions which are necessary for product ordering */
function openBinPopup(tab_id){

	w=window.open("?action=check_continue&tab="+tab_id+"", "BinInfo", "toolbar=no,width=250,height=200,directories=no,status=no,scrollbars=no, resizable=yes, menubar=no, location=no");
}
function addToBin(form_id) {
	document.forms[form_id].submit();
}

function pageStatus() {
    if( document.getElementById('is_loaded') ){
    	document.getElementById('is_loaded').value=1;
    }
}

function ShowGalleryImage($image)
{
	var win=window.open( $image, "aaa", "width=480,height=480,resizable=yes,scrolling=yes");
	win.focus();
}

function flashPopup(id){
	if( typeof _flashPopup == "function"){
		_flashPopup(id);
	} else {
		window.open("?action=video&id="+id, "", "toolbar=no,width=715,height=405,directories=no,status=no,scrollbars=no, resizable=yes, menubar=no, location=no");
	}
}

function flashPopupNew(id, cid){
	window.open("?action=video&cid="+cid+"&id="+id, "", "toolbar=no,width=715,height=405,directories=no,status=no,scrollbars=no, resizable=yes, menubar=no, location=no");
}

function orderSubmode(submode, mode, cid){
//	alert(cid);

	document.getElementById('new_country_id').value=cid;
	//alert(document.getElementById('new_country_id'));
	//alert(cid);
	document.getElementById('order_step').value=mode;
	document.getElementById('submode').value=submode;
	document.forms['bin'].submit();
}

/**
 * used in audio_popup page only
 */
function newWin(url, width, height) {
	var winNew = window.open( url, "winFullScreen", "width="+ width +",height="+ height +",resizable,scrolling,scrollbars" );
 	winNew.focus();
}

function openIngredients(p_id, cid)
{
	 wnd = window.open("?action=product&subact=ingredient&p_id="+p_id+"&cid="+cid, "Ingredients", "toolbar=no,width=300,height=250,directories=no,status=no,scrollbars=yes,resizable=no,menubar=no");
}

function getElementPosition(elemId)
{
	var elem = document.getElementById(elemId);

	var w = elem.offsetWidth;
	var h = elem.offsetHeight;

	var l = 0;
	var t = 0;

	while (elem)
	{
		l += elem.offsetLeft;
		t += elem.offsetTop;
		elem = elem.offsetParent;
	}

	return {"left":l, "top":t, "width": w, "height":h};
}


function getCookie(c_name){
	if(document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

function checkCookie(name, callback, isSelf , cid){
	if(typeof(name) == 'undefuned' || name == '' ){
		return false;
	}

	if(typeof(isSelf) != 'undefined'){
		$j.getJSON('?tmcookies=1&tmcookiesname=' + name + '&cid=' + cid, {}, function(json){ if(json == 'ok'){
			if(typeof(callback != 'undefined')){
				try{
					cook=getCookie(name);
					if (cook!=null && cook!=""){
						eval(callback);
					}else{
					  //alert('Enable the support cookies please! It is necessary for further work!');
						showCookiePopup();
					}
				}catch(e){}
			}

		}});
	}else{
		cook=getCookie(name);
		if (cook!=null && cook!=""){
		  return true;
		}else{
		  //alert('Enable the support cookies please! It is necessary for further work!');
			showCookiePopup();
		  return false;
		}
	}
}

function displayMode(id){
	vs = $j('#'+ id).css('display');
	if( typeof(vs) == null || vs == 'none'){
	    $j('#' + id).show();
	}else{
	    $j('#' + id).hide();
	}
}

function showBlock(id){
	$j('#' + id).show();
}

function hideBlock(id){
	$j('#' + id).css('display','none');
}

var interested = {
	cultivate: function(data){
		$j.each(data, function (key, value){
			switch(key){
				case 'alert':
						alert(value);
					break;
				case 'update':
						$j.each(value, function(id, data){
							switch(data.action){
								case 'html':
										$j('#' + data.id).html(data.val);
									break;
								case 'val':
									$j('#' + data.id).val(data.val);
									break;
							}
						});
					break;
				case 'eval':
						eval(value);
					break;

			}
		});

	},
	personSave: function(id){
		data = $j('#' + id).serialize();
		this.doAjax('person_save', data, 'person_data_section_info', 'html', id);
	},

	onlineSave: function(id){
		data = $j('#' + id).serialize();
        this.doAjax('online_save', data, 'person_online_info', 'html', id);
	},

	conferenceSave: function(id){
		data = $j('#' + id).serialize();
		this.doAjax('conference_save', data, 'person_conference_info', 'html', id);
	},

	partnerSave: function(id){
		data = $j('#' + id).serialize();
		this.doAjax('partner_save', data, 'person_teampartner_info', 'html', id);
	},
    
   	academySave: function(id){
		data = $j('#' + id).serialize();
		this.doAjax('academy_save', data, 'person_academy_info', 'html', id);
	},


	doAjax:function(action, data, key, dataType, id){
		$j("#loader_"+id).css('visibility', 'visible');
		switch(dataType){
			case 'html':
					$j.getJSON(window.location.href + '&saction=check_state', {}, function(json){
						if(json.state=='ok'){
							$j.get(window.location.href + '&saction='+ action + '&data=' + data, function(data){$j('#' + key).html(data);});
						}
						$j("#loader_"+id).css('visibility', 'hidden')
					});

				break;
			default:
					$j.getJSON(window.location.href + '&saction='+ action + '&data=' + data, {}, function(json){interested.cultivate(json);});
				break;
		}

	}
}

function getContent(id){
	if(id != ''){
		$j.get(window.location.href + '&saction=get_seminar_content&id=' + id, function(data){$j('#online_data_address').html(data);});
	} else {
		$j('#online_data_address').html('');
	}
}