/*!______________________________________________________________________*\
||                                                                      ||
|| __   __                        				        ||
|| \ \ / /__  _ __   ___ __ _ 		- Fırat Ağdaş                   ||
||  \ V / _ \| '_ \ / __/ _` |		- Yonca New Media               ||
||   | | (_) | | | | (_| (_| |		- firat@yonca-ad.com            ||
||   |_|\___/|_| |_|\___\__,_|                                          ||
||                                                                      ||
||______________________________________________________________________||
\*                                                                      */

function dropdown_register()
{
	if (!arguments || arguments.length == 0)
	{
		return false;
	}
	
	for (var i = 0; i < arguments.length; i++)
	{
		if (Dom.get(arguments[i] + '_menu'))
		{
			menu_register(arguments[i]);	
		}
	}
}

function menu_register(menuid, right, skip)
{
	var over = false, timehandler = null, linkobj = Dom.get(menuid), right = right || false, skip = skip || false;
	if (!linkobj)
	{
		if (!skip)
		{
			Yonca.domready_register(function(){ menu_register(menuid, right, true); });
		}	
		return false;
	}
	
	linkobj.hover = false;
	if (right)
	{
		var ptags = Yonca.fetch_parent_obj('div', linkobj, function(obj)
		{ 
			if (obj.className == 'dropdown')
			{ 
				return true;
			}
			
			return false;
		});
		if (ptags)
		{
			Yonca.domready_register(function()
			{
				ptags = Dom.get(ptags.id.replace(/_menu$/, ''));
			});
		}
	}
	
	function alt_hover_restore(hover)
	{
		if (right && ptags)
		{
			ptags.hover = hover ? true : false;
			if (hover)
			{
				Dom.addClass(linkobj, 'narr-hover');
				Dom.addClass(linkobj, 'dropdown_ahover');
			}
			else
			{
				Dom.removeClass(linkobj, 'narr-hover');	
				Dom.removeClass(linkobj, 'dropdown_ahover');
			}
		}
	}
	
	Event.on(linkobj, 'mouseover', function(e)
	{
		var menuobj = Dom.get(menuid + '_menu'), coords = Dom.getXY(this);
		Dom.setStyle(menuobj, 'display', '');
		if (right)
		{
			var ow = is_rtl ? (-1 * menuobj.offsetWidth) : this.offsetWidth, dif = is_rtl ? -2 : 2;
			coords[0] += ow;
			coords[1] -= dif;
		}
		else
		{
			// coords[0] -= Math.round((menuobj.offsetWidth - this.offsetWidth) / 2);
			coords[0] -= 10;
			coords[1] += 22;
		}
		
		Dom.setXY(menuobj, [coords[0], coords[1]], false);
		Dom.addClass(this, 'ahover');

		this.hover = true;
		alt_hover_restore(true);
	});
	
	function hide_menu(menuid)
	{
		if (!linkobj.hover)
		{
			Dom.setStyle(menuid + '_menu', 'display', 'none');
			Dom.removeClass(menuid, 'ahover');
		}
		else
		{
			clearTimeout(timehandler);
			timehandler = setTimeout(function(){ hide_menu(menuid); }, 100);
		}
	}
	
	Event.on(linkobj, 'mouseout', function(e)
	{
		var idname = this.id;
		clearTimeout(timehandler);
		timehandler = setTimeout(function(){ hide_menu(idname); }, 100);
		this.hover = false;
		alt_hover_restore(false);
	});
	
	Event.on(menuid + '_menu', 'mouseover', function(e)
	{
		linkobj.hover = true;
		alt_hover_restore(true);
	});
	
	Event.on(menuid + '_menu', 'mouseout', function(e)
	{
		clearTimeout(timehandler);
		timehandler = setTimeout(function(){ hide_menu(menuid); }, 100);
		linkobj.hover = false;
		alt_hover_restore(false);
	});
	if (menuid == 'navdropdown' || menuid == 'navdropdown_footer')
	{
		// Event.on(linkobj, 'click', function(e){ Event.preventDefault(e); });
	}
}

Yonca.domready_register(function(){
	var s1 = new Yonca_Scrolling();
	s1.scroll_height = 148;
	s1.auto_mode = true;
	s1.init('vitrine');
	
	var s2 = new Yonca_Scrolling();
	s2.scroll_height = 110;
	s2.auto_mode = true;
	s2.scroll_delay = 7000;
	s2.scroll_method = YAHOO.util.Easing.easeBothStrong;
	s2.button_register = true;
	s2.init('news');
	
	var s3 = new Yonca_Scrolling();
	s3.scroll_height = 190;
	s3.auto_mode = true;
	s3.init('vitrine2');
	
	var s4 = new Yonca_Scrolling();
	s4.scroll_height = 111;
	s4.auto_mode = true;
	s4.scroll_delay = 7000;
	s4.scroll_method = YAHOO.util.Easing.easeBothStrong;
	s4.button_register = true;
	s4.init('news2');
	
	var s5 = new Yonca_Scrolling();
	s5.scroll_height = 30;
	s5.auto_mode = true;
	s5.scroll_delay = 9000;
	s5.scroll_method = YAHOO.util.Easing.easeBothStrong;
	s5.init('announce');
	
	var hover = new Yonca_Product_Hovers();
	hover.init();
	
	var obj = Dom.get('theman_flash');
	if (obj)
	{
		Dom.setStyle(obj, 'visibility', 'visible');
		var locateobj = function(obj) {	
			var coords = Dom.getXY('price_list_link');
			Dom.setXY(obj, [coords[0] - Math.round(obj.offsetWidth / 5), coords[1] - obj.offsetHeight], false);
		};
		
		locateobj(obj);
		Event.on(window, 'resize', function(e){ setTimeout(function(){ locateobj(obj); }, 0); });
	}
});

function Yonca_Product_Hovers()
{
	this.cache = new Array();
	this.actives = new Array();
	this.active_progress = null;
	this.controltimer = null;
}

Yonca_Product_Hovers.prototype = {
	init: function()
	{
		var obj = Dom.get('home_product_blocks');
		if (!obj)
		{
			return false;
		}
		
		var blocks = Dom.getElementsBy(function(_this){ return Dom.hasClass(_this, 'phbubble'); }, 'div', obj);
		for (var i = 0; i < blocks.length; i++)
		{
			var newblock = blocks[i].cloneNode(true);
			
			Dom.replaceClass(newblock, 'phbubble', 'phbubble_big');
			Dom.addClass(newblock, 'big-popup');
			
			Dom.getElementsBy(__true_function, 'img', newblock)[0].removeAttribute('width');
			Dom.setStyle(Dom.getElementsByClassName('hpb_detail', 'div', newblock)[0], 'display', 'none');
			
			var hpbs = Dom.getElementsByClassName('hpb_name', 'div', newblock);
			for (var k = 0; k < hpbs.length; k++)
			{
				Dom.setStyle(hpbs[k], 'display', '');
			}
			
			Dom.setStyle(newblock, 'display', 'none');
			Dom.setStyle(newblock, 'opacity', 0);
			Dom.setStyle(newblock, 'position', 'absolute');
			Dom.setStyle(newblock, 'z-index', '9999');
			Dom.setStyle(newblock, 'top', '-9999px');
			Dom.setStyle(newblock, 'left', '-9999px');
			
			this.cache[i] = newblock;
			
			document.body.appendChild(newblock);
			
			Event.on(blocks[i], 'mouseover', this.mouseover, [blocks[i], i], this, true);
			Event.on(newblock, 'mouseover', this.mouseover2, this, true);
			Event.on(newblock, 'mouseout', this.mouseout, i, this, true);
		}
	},
	
	mouseover2: function()
	{
		clearTimeout(this.controltimer);	
	},
	
	mouseover: function(e, args)
	{
		var obj = args[0], id = args[1];
		if (this.active_progress)
		{
			var el = this.active_progress.getEl();
			this.active_progress.stop();
			if (el != this.cache[id])
			{
				this.hide(el);
			}
			else
			{
				return false;
			}
		}
		
		var coords = Dom.getXY(obj);
		
		Dom.setStyle(this.cache[id], 'display', '');
		
		coords[0] -= Math.round((this.cache[id].offsetWidth - obj.offsetWidth) / 2);
		coords[1] -= Math.round((this.cache[id].offsetHeight - obj.offsetHeight) / 2);
		
		var _this = this;
		
		Dom.setXY(this.cache[id], coords, false);
		this.active_progress = new YAHOO.util.Anim(this.cache[id], { opacity: { to: 1 } }, 0.3, YAHOO.util.Easing.easeBothStrong);
		this.active_progress.onComplete.subscribe(function()
		{
			var el = this.getEl();
			if (Dom.getStyle(el, 'display') == 'none')
			{
				Dom.setStyle(el, 'opacity', 0);
			}
			
			el.inProgress = false;
			_this.active_progress = null;
		});
		this.active_progress.animate();
		
		this.controltimer = setTimeout(function(){ _this.hide(id); }, 500);
	},
	
	mouseout: function(e, id)
	{
		this.hide(id);
	},
	
	hide: function(id)
	{
		var obj = Lang.isObject(id) ? id : this.cache[id];
		
		Dom.setStyle(obj, 'display', 'none');
		Dom.setStyle(obj, 'opacity', 0);
		Dom.setStyle(obj, 'top', '-9999px');
		Dom.setStyle(obj, 'left', '-9999px');
	}
};
