// user's functions
function aProcClick(idP, idC, idB){
    advClick('http://www.amiro.ru/aproc.php?action=click&place='+idP+'&cmpg='+idC+'&id='+idB+'&referer=');
}

function rotateBanners(){
    advPlacesContent = new Array();
    fillAdvPlaceWatcher = new Array();
    
    var rndseed = new String(Math.random());
    var newBanners = document.createElement('script');
    newBanners.src = 'http://amiro.ru/aproc.php?action=showall&places='+processShownAdvPlaces(shownAdvPlaces)+'&rs='+advCurTmStamp.getTime()+rndseed.substring(2,11)+'&curl=http://amiro.ru/&referer='+encodeURIComponent(document.referrer);
    document.body.appendChild(newBanners);
}

function setEventHandlerDON(eventName, eventElement, handler){
    if(typeof(eventElement.addEventListener) != 'undefined'){
        eventElement.addEventListener(eventName, handler, false);
    }else if(eventElement.attachEvent){
        eventElement.attachEvent('on' + eventName, handler);
    }
}

var amiSlidres = {
    getObjectAttribute : function(oTarget, name, isBool){
        var value = oTarget.getAttribute(name);
        if(value == null){
            value = '';
        }
        if(isBool){
            return value != '' && value != '0';
        }else{
            return value;
        }
    },
    
    showBlockDetails : function(oBlock){
        var oBlockDetails = oBlock.getElementsByTagName('div')[1];
        var oBlockDetailsContainer = oBlockDetails.getElementsByTagName('div')[0];
        this.startBlockShow(oBlockDetails, oBlockDetailsContainer.offsetHeight, 1);
    },

    hideBlockDetails : function(oBlock){
        var oBlockDetails = oBlock.getElementsByTagName('div')[1];
        var oBlockDetailsContainer = oBlockDetails.getElementsByTagName('div')[0];
        this.startBlockShow(oBlockDetails, oBlockDetailsContainer.offsetHeight, -1);
    },
    
    startBlockShow : function(oTarget, maxHeight, direction, isStart){
        if(isStart == undefined){
            isStart = false;
        }
        var currentDirection = this.getObjectAttribute(oTarget, 'currentDirection');
        var currentTimeout = '';
        if(isStart){
            clearInterval(this.getObjectAttribute(oTarget, 'currentInterval'));
            oTarget.setAttribute('currentDirection', direction);
            oTarget.setAttribute('currentInterval', setInterval(function(oThis, oTargetBlock, maxHeight){return function(){oThis.changeBlockHeight(oTargetBlock, maxHeight, direction)}}(this, oTarget, maxHeight), 10));
        }else{
            clearTimeout(this.getObjectAttribute(oTarget, 'currentTimeout'));
            if(direction != this.getObjectAttribute(oTarget, 'currentDirection')){
                oTarget.setAttribute('currentTimeout', setTimeout(function(param1, param2, param3, param4){return function(){param4.startBlockShow(param1, param2, param3, true)}}(oTarget, maxHeight, direction, this), (direction == -1 ? 70 : 70)));
            }
        }
    },
    
    changeBlockHeight : function(oTargetBlock, maxHeight, direction){
        var currentHeight = oTargetBlock.offsetHeight;
        if(direction > 0 && currentHeight < maxHeight || direction < 0 && currentHeight > 1){
            currentHeight += direction * Math.max(1, direction * Math.floor((direction > 0 ? maxHeight : 0) - currentHeight) * 0.4);
            currentHeight = Math.min(currentHeight, maxHeight);
            currentHeight = Math.max(currentHeight, 1);
            oTargetBlock.style.height = currentHeight + 'px';
        }
        if(direction > 0 && currentHeight == maxHeight || direction < 0 && currentHeight == 1){
            clearInterval(this.getObjectAttribute(oTargetBlock, 'currentInterval'));
            oTargetBlock.setAttribute('currentDirection', '');
        }
    }
}

var imgswapa = new Image(); imgswapa.src = '_mod_files/ce_images/swaper1.gif';
var imgswapb = new Image(); imgswapb.src = '_mod_files/ce_images/swaper2.gif';

function swap(numid) {
    var blockObj = document.getElementById('swaping'+numid);
    if(blockObj != null){
        if(blockObj.style.display == 'block'){
            blockObj.style.display = 'none';
            document.images['swaper'+numid].src = imgswapb.src;
        }else{
            blockObj.style.display = 'block';
            document.images['swaper'+numid].src = imgswapa.src;
        }
    }
}


var deltaScroll = 300;
var wheelInterval = null;
var onClickLeft = null;
var onClickRight = null;


function tl3_makeScroll(where) {
	var lenta_parent = document.getElementById('portfolio_preview_inner');
	var lenta = document.getElementById('portfolio_preview_inner2');
	
	var last=0;
	var lastScroll=0;
	var b=0;
		
	if (!lenta.style.left) lenta.style.left='0px';

	if (where=='right') {
		if (Math.abs(parseInt(lenta.style.left)) < (lenta.offsetWidth - lenta_parent.offsetWidth)) {
			if (Math.abs(parseInt(lenta.style.left)) + deltaScroll > lenta.offsetWidth - lenta_parent.offsetWidth) {
				last=1;
			}
			b=1;
		}
	} else if (where=='left') {
		if (parseInt(lenta.style.left) < 0) {
			if (parseInt(lenta.style.left) + deltaScroll > 0) {
				last = 1;
			}
			b=1;
		}
	}
	
	if (b==1) {
		onClickLeft = document.getElementById('tl3_left').onclick;
		document.getElementById('tl3_left').onclick = function(){ return false; };
		onClickRight = document.getElementById('tl3_right').onclick;
		document.getElementById('tl3_right').onclick = function(){ return false; };

		if (where=='right') { 
			if (last == 1) {
				lastScroll = (lenta.offsetWidth - lenta_parent.offsetWidth) - Math.abs(parseInt(lenta.style.left));
				var nextPos = parseInt(lenta.style.left) - lastScroll;
			} else {
				var nextPos = parseInt(lenta.style.left) - deltaScroll;
			}
			
			wheelInterval = setInterval(function() { tl3_makeWheel(lenta, where, parseInt(lenta.style.left), nextPos, -20)}, 30); 
		}
		else if (where=='left') { 
			if (last == 1) {
				lastScroll = Math.abs(parseInt(lenta.style.left));
				var nextPos = parseInt(lenta.style.left) + lastScroll;
			} else {
				var nextPos = parseInt(lenta.style.left) + deltaScroll;
			}
			
			wheelInterval = setInterval(function() { tl3_makeWheel(lenta, where, parseInt(lenta.style.left), nextPos, 20)}, 30); 
		}
	}
	
	return false;
}

function tl3_makeWheel(what, where, xFrom, xTo, step) {
	if (where=='right') {
		if ((xFrom + step) > xTo) {
			what.style.left = parseInt(what.offsetLeft + step) + 'px';
		} else {
			//last step
			what.style.left = parseInt(what.offsetLeft + (xTo - xFrom)) + 'px';
			clearInterval(wheelInterval);
			wheelInterval = null;
			
			document.getElementById('tl3_right').onclick = onClickRight;
			document.getElementById('tl3_left').onclick = onClickLeft;
		}
		
	} else if (where=='left') {
		if ((xFrom + step) < xTo) {
			what.style.left = parseInt(what.offsetLeft + step) + 'px';
		} else {
			//last step
			what.style.left = parseInt(what.offsetLeft - (xFrom - xTo)) + 'px';
			clearInterval(wheelInterval);
			wheelInterval = null;
			
			document.getElementById('tl3_right').onclick = onClickRight;
			document.getElementById('tl3_left').onclick = onClickLeft;
		}
	}
}

function onDownloadClick(id){
    if(pageTracker){
	var gCategory = '';
	var gAction = 'Download';
	var gValue = '';
	switch(parseInt(id)){
	    case 117: url = '/downloads/free/windows/freecms'; gCategory = 'Packages'; gValue = 'pkgWindowsFree'; break;
            case 124: url = '/downloads/trials/windows/vizitka'; gCategory = 'Packages'; gValue = 'pkgWindowsVizitka'; break;
            case 116: url = '/downloads/trials/windows/corporate'; gCategory = 'Packages'; gValue = 'pkgWindowsCorporate'; break;
            case 125: url = '/downloads/trials/windows/vitrina'; gCategory = 'Packages'; gValue = 'pkgWindowsVitrina'; break;
            case 118: url = '/downloads/trials/windows/minimarket'; gCategory = 'Packages'; gValue = 'pkgWindowsMinimarket'; break;
            case 115: url = '/downloads/trials/windows/business'; gCategory = 'Packages'; gValue = 'pkgWindowsBudiness'; break;

            case 106: url = '/downloads/utilities/zend_test'; gCategory = 'Packages'; gValue = 'pkgZendTest'; break;
            case 105: url = '/downloads/utilities/php_installer_402'; gCategory = 'Packages'; gValue = 'pkgPHPInstaller402'; break;
            case 158: url = '/downloads/utilities/php_installer_506'; gCategory = 'Packages'; gValue = 'pkgPHPInstaller506'; break;

    	    case 174: url = '/downloads/free/unix/installer'; gCategory = 'Packages'; gValue = 'pkgPHPInstaller'; break;
    	    case 128: url = '/downloads/free/unix/freecms'; gCategory = 'Packages'; gValue = 'pkgUnixFree'; break;
            case 130: url = '/downloads/trials/unix/vizitka'; gCategory = 'Packages'; gValue = 'pkgUnixVizitka'; break;
            case 127: url = '/downloads/trials/unix/corporate'; gCategory = 'Packages'; gValue = 'pkgUnixCorporate'; break;
            case 131: url = '/downloads/trials/unix/vitrina'; gCategory = 'Packages'; gValue = 'pkgUnixVitrina'; break;
            case 129: url = '/downloads/trials/unix/minimarket'; gCategory = 'Packages'; gValue = 'pkgUnixMinimarket'; break;
            case 126: url = '/downloads/trials/unix/business'; gCategory = 'Packages'; gValue = 'pkgUnixBusiness'; break;

            case 168: url = '/downloads/movies/view_flv/se'; gAction = 'ViewFLV'; gCategory = 'Movies'; gValue = 'movFLVSE'; break;
            case 169: url = '/downloads/movies/view_flv/add_page'; gAction = 'ViewFLV'; gCategory = 'Movies'; gValue = 'movFLVAddPage'; break;
            case 171: url = '/downloads/movies/view_flv/modules'; gAction = 'ViewFLV'; gCategory = 'Movies'; gValue = 'movFLVModules'; break;
            case 170: url = '/downloads/movies/view_flv/add_image'; gAction = 'ViewFLV'; gCategory = 'Movies'; gValue = 'movFLVAddImage'; break;

            case 42: url = '/downloads/movies/view/add_news'; gAction = 'View'; gCategory = 'Movies'; gValue = 'movAddNews'; break;
            case 44: url = '/downloads/movies/view/add_page'; gAction = 'View'; gCategory = 'Movies'; gValue = 'movAddPage'; break;
            case 45: url = '/downloads/movies/view/change_logo_name'; gAction = 'View'; gCategory = 'Movies'; gValue = 'movChangeLogoName'; break;
            case 47: url = '/downloads/movies/view/add_image_link_file'; gAction = 'View'; gCategory = 'Movies'; gValue = 'movAddImageLinkFile'; break;
            case 49: url = '/downloads/movies/view/paste_from_word'; gAction = 'View'; gCategory = 'Movies'; gValue = 'movPasteFromWord'; break;
            case 51: url = '/downloads/movies/view/add_visit_counter'; gAction = 'View'; gCategory = 'Movies'; gValue = 'movAddVisitCounter'; break;

            case 164: url = '/downloads/movies/flv/se'; gCategory = 'Movies'; gValue = 'movFLVSE'; break;
            case 165: url = '/downloads/movies/flv/add_page'; gCategory = 'Movies'; gValue = 'movFLVAddPage'; break;
            case 167: url = '/downloads/movies/flv/modules';gCategory = 'Movies'; gValue = 'movFLVModules'; break;
            case 166: url = '/downloads/movies/flv/add_image'; gCategory = 'Movies'; gValue = 'movFLVAddImage'; break;

            case 41: url = '/downloads/movies/add_news'; gCategory = 'Movies'; gValue = 'movAddNews'; break;
            case 43: url = '/downloads/movies/add_page'; gCategory = 'Movies'; gValue = 'movAddPage'; break;
            case 46: url = '/downloads/movies/change_logo_name'; gCategory = 'Movies'; gValue = 'movChangeLogoName'; break;
            case 48: url = '/downloads/movies/add_image_link_file'; gCategory = 'Movies'; gValue = 'movAddImageLinkFile'; break;
            case 50: url = '/downloads/movies/paste_from_word'; gCategory = 'Movies'; gValue = 'movPasteFromWord'; break;
            case 52: url = '/downloads/movies/add_visit_counter'; gCategory = 'Movies'; gValue = 'movAddVisitCounter'; break;

            case 101: url = '/downloads/presents/select_platform'; gAction = 'View'; gCategory = 'Presentations'; gValue = 'pptSelectPlatform'; break;
            case 99: url = '/downloads/presents/strategy'; gAction = 'View'; gCategory = 'Presentations'; gValue = 'pptStrategy'; break;
            case 98: url = '/downloads/presents/web_developers_selection'; gAction = 'View'; gCategory = 'Presentations'; gValue = 'pptWebDevelopersSelection'; break;
            case 100: url = '/downloads/presents/six_groups'; gAction = 'View'; gCategory = 'Presentations'; gValue = 'pptSixGroups'; break;

            case 139: url = '/downloads/press_kit/trademark'; gCategory = 'PressKit'; gValue = 'fileTrademark'; break;
            case 140: url = '/downloads/press_kit/about_full'; gCategory = 'PressKit'; gValue = 'fileAboutFull'; break;
            case 141: url = '/downloads/press_kit/about_brief'; gCategory = 'PressKit'; gValue = 'fileAboutBrief'; break;
            case 145: url = '/downloads/press_kit/amiro_presentation'; gCategory = 'PressKit'; gValue = 'fileAmiroPresentation'; break;
            case 153: url = '/downloads/press_kit/screenshots'; gCategory = 'PressKit'; gValue = 'fileScreenshots'; break;
            case 154: url = '/downloads/press_kit/buklet_builder'; gCategory = 'PressKit'; gValue = 'fileBukletBuilder'; break;
            case 155: url = '/downloads/press_kit/buklet_success'; gCategory = 'PressKit'; gValue = 'fileBukletSuccess'; break;
            case 156: url = '/downloads/press_kit/buklet_partnersheep'; gCategory = 'PressKit'; gValue = 'fileBukletPartnership'; break;

	    default: url = '/unknown/'; gAction = 'Unknown'; gCategory='Unknown'; gValue = 'Unknown'; break;
	}
	if(url != ''){
	    //pageTracker._trackPageview(url);
	    //alert(gCategory+' / '+gAction+' / ItemName / '+gValue);
	    //alert(pageTracker._trackEvent);
	    pageTracker._trackEvent(gCategory, gAction, gValue);
	}
    }
}

var objVisible = {};
var thisIe = false;
var thisIe6 = false;
var thisOpera = false;
var thisApple = false;
var thisFF = false;
var animActive = false;

var show_custom_tab_curr = 1;
var show_custom_tab_timer = setInterval(function(){
	if(show_custom_tab_curr>3) show_custom_tab_curr = 1;
	show_custom_tab(show_custom_tab_curr);
	show_custom_tab_curr++;
}, 8000);
function show_custom_tab_ctimer() {
	clearInterval(show_custom_tab_timer);
}

function docOnLoad() {
	var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel";
	obj = document.getElementById("portfolio_preview_inner");
	scrollerSetEvent(obj, mousewheelevt, function(evt){
		var wheelDelta = 0;
		if(!evt) evt = window.event;
		if (evt.wheelDelta){
	    	wheelDelta = Math.round(evt.wheelDelta/120);
	    } else if (evt.detail){
	    	wheelDelta = Math.round(-evt.detail/3);
	    }
		if(wheelDelta==1 && wheelInterval==null) {
			tl3_makeScroll('left');
		}
		if(wheelDelta==-1 && wheelInterval==null) {
			tl3_makeScroll('right');
		}
		// Вспомогательня функция обработки mousewheel
        if (wheelDelta) {
                //handle(wheelDelta);
                // Отменим текущее событие - событие поумолчанию (скролинг окна).
                if (evt.preventDefault) evt.preventDefault();
                evt.returnValue = false; // для IE
        }
		return false;
	});
}
function scrollerSetEvent(elem, type, handle) {
	if(elem.addEventListener) { elem.addEventListener(type, handle, false); }
	else if(elem.attachEvent) { elem.attachEvent("on"+type, function() { handle.call(elem) }); }
}

function show_custom_tab(tab_name) {
	if(!animActive) {
		tab1 = document.getElementById("custom_tab_1");
		tab2 = document.getElementById("custom_tab_2");
		tab3 = document.getElementById("custom_tab_3");

		link1 = document.getElementById("custom_tab_link_1");
		link2 = document.getElementById("custom_tab_link_2");
		link3 = document.getElementById("custom_tab_link_3");

		if(tab1!==null) {
			if(tab1.style.display=="block") objVisible = tab1;
			if(tab2.style.display=="block") objVisible = tab2;
			if(tab3.style.display=="block") objVisible = tab3;
		
			if(/MSIE/.test(navigator.userAgent)) thisIe = true;
			if(/MSIE (5\.5|6)/.test(navigator.userAgent)) thisIe6 = true;
			if(/Opera/.test(navigator.userAgent)) thisOpera = true;
			if(/AppleWebKit/.test(navigator.userAgent)) thisApple = true;
			if(/Firefox/.test(navigator.userAgent)) thisFF = true;
		
			if(tab_name==1 && tab1.style.display=="none") {
				tabAnim.show_tab(tab1);
				link1.className = "swaphead_s";
				link2.className = "swaphead";
				link3.className = "swaphead";
			}
			else if(tab_name==2 && tab2.style.display=="none") {
				tabAnim.show_tab(tab2);
				link1.className = "swaphead";
				link2.className = "swaphead_s";
				link3.className = "swaphead";
			}
			else if(tab_name==3 && tab3.style.display=="none") {
				tabAnim.show_tab(tab3);
				link1.className = "swaphead";
				link2.className = "swaphead";
				link3.className = "swaphead_s";
			}
		}
	}
	return false;
}
var tabAnim = {
	anim_speed: 300,
	defH: 114,
	
	// Private
	anim_h: 0,
	anim_h2: 0,
	anim_spd: 0,
	animDefH: 0,
	
	show_tab: function(obj) {
		if(thisIe) objVisible.style.height = "113px";
		else if(thisFF) objVisible.style.height = "106px";
		else if(thisApple) objVisible.style.height = "107px";
		else objVisible.style.height = "107px";

		obj.style.display = "block";
		tabAnim.chSize(obj, tabAnim.anim_speed, tabAnim.defH, 0);
	},
	chSize: function(obj, speed, height, timer, anim_spd, anim_h, anim_h2, anim_h3, animDefH) {
		if(timer==0) {
			animActive = true;
			cycle = speed / 24;
			anim_spd = speed / cycle;
			anim_h = (height - obj.offsetHeight) / cycle;
			anim_h2 = obj.offsetHeight;
			anim_h3 = objVisible.offsetHeight;
			animDefH = anim_h2;
			timer++;
		}
		
		if(timer<=cycle) {
			anim_h2+= anim_h;
			anim_h3-= anim_h;

			if(anim_h3>0) objVisible.style.height = anim_h3 + "px";
			obj.style.height = anim_h2 + "px";
			
			timer++;
			window.setTimeout(function(){tabAnim.chSize(obj, anim_spd, height, timer, anim_spd, anim_h, anim_h2, anim_h3, animDefH)}, anim_spd);
		}
		else {
			obj.style.height = height+"px";
			objVisible.style.display = "none";
			objVisible.style.height = "1px";
			animActive = false;
		}
	}
};