/**
 * Podpinanie parametru target dla link�w
 * rel = blank | top | self | parent
 */
function externalLinks() 
{
	if (!document.getElementsByTagName) 
	{
		return;
	}
	
	var anchors = document.getElementsByTagName("a");
	
	for (var i=0; i<anchors.length; i++) 
	{
		var anchor = anchors[i];
		
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "blank" || anchor.getAttribute("rel") == "top" || anchor.getAttribute("rel") == "self" || anchor.getAttribute("rel") == "parent"))
		{
			anchor.target = "_" + anchor.getAttribute("rel");
		}
	}
}
window.onload = externalLinks;


/**
 * Detekcja przegladarki
 */
function loadBrowserStyle()
{
	
	if($.browser.name == 'msie' && ($.browser.versionNumber == 6 || $.browser.versionNumber == 7))
	{
		document.write(unescape('%3Clink href="theme/css/style-' + $.browser.name + $.browser.versionNumber + '.css"  type="text/css" rel="stylesheet" /%3E'));
	}
	else if($.browser.name == 'firefox' || $.browser.name == 'opera' || $.browser.name == 'chrome')
	{
		document.write(unescape('%3Clink href="theme/css/style-' + $.browser.name + '.css"  type="text/css" rel="stylesheet" /%3E'));
	}
}
loadBrowserStyle();

var _ActiveContent = 0;
var _ActionPlay    = true;
var _ActionId	   = 0;

/**
 * Obsluga elementow podczas ladowania sie strony
 */
$(document).ready(function() 
{
	// Nawigacja baneru na index
	//
	$("div.bar-top-navi-header a").click(function() 
	{
		_id 	= $("div.bar-top-navi-header a").index(this);
		_count 	= $("div.bar-top-navi-header a").length;
		
		for(i = 0; i < _count; i++)
		{
			$("div.bar-top-navi-header a:eq(" + i + ")").removeClass("first");
			$("div.bar-top-navi-header a:eq(" + i + ")").removeClass("firstactive");
			$("div.bar-top-navi-header a:eq(" + i + ")").removeClass("active");
			
			$("div#navi-content-" + i).css("display", "none");
			//$("div#navi-footer-" + i).css("display", "none");
		}
		$("div.bar-top-navi-header a:eq(0)").addClass("first");
		
		if(_id == 0)
		{
			$("div.bar-top-navi-header a:eq(" + _id + ")").removeClass("first");
			$("div.bar-top-navi-header a:eq(" + _id + ")").addClass("firstactive");
		}
		else
		{
			$("div.bar-top-navi-header a:eq(" + _id + ")").addClass("active");
		}
		
		_ActiveContent = _id;
		
		$("div#navi-content-" + _id).css("display", "block");
		
		//$("div#navi-content-" + _id).scrollable(); 
		
		//$("div#navi-footer-" + _id).css("display", "block");
	});
	
	//$("div#navi-footer-0 div.channel a").click(function() 
	//{
	//	_ActionId = $("div#navi-footer-" + _ActiveContent + " div.channel a").index(this);
	//	naviBoxHome(_ActionId);
	//});
	//$("div#navi-footer-1 div.channel a").click(function() 
	//{
	//	_ActionId = $("div#navi-footer-" + _ActiveContent + " div.channel a").index(this);
	//	naviBoxHome(_ActionId);
	//});
	//$("div#navi-footer-2 div.channel a").click(function() 
	//{
	//	_ActionId = $("div#navi-footer-" + _ActiveContent + " div.channel a").index(this);
	//	naviBoxHome(_ActionId);
	//});
	//$("div#navi-footer-3 div.channel a").click(function() 
	//{
	//	_ActionId = $("div#navi-footer-" + _ActiveContent + " div.channel a").index(this);
	//	naviBoxHome(_ActionId);
	//});
	
	/** Obsługa wysokości treściw IE6 */
	if($.browser.name == 'msie' && $.browser.versionNumber == 6)
	{
		heightContent 	= 0;
		heightLeft 		= $("div#column-left").height();
		heightCenter 	= $("div#column-center").height();
		heightRight 	= $("div#column-right").height();
		
		if(heightContent < heightLeft) 	 heightContent = heightLeft;
		if(heightContent < heightCenter) heightContent = heightCenter;
		if(heightContent < heightRight)  heightContent = heightRight;
		
		$("div#content").height(heightContent);
	}
	
	/** Obsługa pól select */
	$("select#basket-action").select_skin();
	
	/** Obsługa FAQ */
	var openLi = -1;
	
	$("ol#faq li").click(function() 
	{
		e = $("ol#faq li").index(this);
		
		if(openLi != e)
		{
			if (openLi >= 0) {
				$("ol#faq li:eq(" + openLi + ") div.faqContent").hide(200);
			}
			
			$("ol#faq li:eq(" + e + ") div.faqContent").show(200);
			
			openLi = e;
		}
	});
	
	/** Poleć znajomemu */
	$("a#view-to-frend").click(function() 
	{
		$("div#mail-to-frend").show(300);
	});
	
	$("img#send-to-frend").click(function() 
	{
		$("div#send-error").hide();
		$("div#send-info").hide();
		
		mail 	= $("#email_frend").val();
		token 	= $("#token").val();
		id 		= $("#frend_id").val();
		name 	= $("#frend_name").val();
		
		// Wysłanie polecenia
		$.ajax({ 
			type: 		"POST",
			url: 		"/engine/lib/ajax.php",
			data: 		"mail=" + mail + "&token=" + token + "&id=" + id + "&name=" + name,
			
			success: 	function(msg)
			{
				//<div id="send-error" class="display-none info-box info-box-error"></div>
				//<div id="send-info" class="display-none info-box info-box-info"></div>
				if(msg == 'error_token')
				{
					$("div#send-error").html('Błędny kod z obrazka!');
					$("div#send-error").show(300);
				}
				else if(msg == 'error_mail')
				{
					$("div#send-error").html('Błędny adres e-mail!');
					$("div#send-error").show(300);
				}
				else if(msg == 'error_user')
				{
					$("div#send-error").html('Aby wysłać polecenie musisz być zalogowany!!');
					$("div#send-error").show(300);
				}
				else if(msg == 'error_send')
				{
					$("div#send-error").html('Wystąpił błąd!');
					$("div#send-error").show(300);
				}
				else if(msg == 'OK')
				{
					$("#email_frend").val('');
					$("#token").val('');
					$("#frend_id").val('');
					$("#frend_name").val('');
					
					$("div#send-info").html('Polecenie produktu zostało wysłana na podany mail.');
					$("div#send-info").show(300);
					
					$("#form-to-frend").hide();
				}
				else
				{
					alert(msg);	
				}
			},
			error: function(xhr, desc, e)
			{
				// Komunikat
				alert("Wystąpił błąd z wysłaniem polecenia!");
			}
		});
	});
	
});

/**
 * BOK nawigacjna na stronie glownej
 * 
 */
function naviBoxPlay()
{
	return false;
	//alert($("div#navi-footer-" + _ActiveContent + " div.channel a").length);
	
	if(_ActionPlay == true)
	{
		
		if(_ActionId > ($("div#navi-footer-" + _ActiveContent + " div.channel a").length - 2))
		{
			_ActionId = 0;
		}
		
		naviBoxHome(_ActionId);
		_ActionId++;
		
	}
	
	setTimeout("naviBoxPlay();", 5000);
}

//naviBoxPlay();

function naviBoxHome(_id)
{
	return false;
	//alert(' ID: ' + _id);
	
	//$("div#navi-footer-0 div.channel a").click(function() 
	//{
	
		//_id 	= $("div#navi-footer-0 div.channel a").index(box);
		
		_count 	= $("div#navi-footer-" + _ActiveContent + " div.channel a").length;
		_boxC	= $("div#navi-content-" + _ActiveContent + " div.box-index").length;
		
		_active = 0;
		
		//alert(_boxC);
		
		for(i = 0; i < _boxC; i++)
		{
			if($("div#navi-content-" + _ActiveContent + " div.box-index:eq(" + i + ")").css("display") == 'block');
			{
				_active = i;
				//break;
			}
			
			$("div#navi-content-" + _ActiveContent + " div.box-index:eq(" + i + ")").css("display", "none");
		}
		
		//alert(_active);
		
		$("div#navi-content-" + _ActiveContent + " div.box-index:eq(" + _active + ")").css("display", "none");
		
		if(_id == 0)
		{
			if(_active == 0)
			{
				$("div#navi-content-" + _ActiveContent + " div.box-index:eq(" + _active + ")").css("display", "block");
			}
			else
			{
				$("div#navi-content-" + _ActiveContent + " div.box-index:eq(" + (_active - 1) + ")").css("display", "block");
			}
		}
		else if(_id == (_count - 1))
		{
			if(_active == (_boxC - 1))
			{
				$("div#navi-content-" + _ActiveContent + " div.box-index:eq(" + (_boxC - 1) + ")").css("display", "block");
			}
			else
			{
				$("div#navi-content-" + _ActiveContent + " div.box-index:eq(" + (_active + 1) + ")").css("display", "block");
			}
		}
		else
		{
			$("div#navi-content-" + _ActiveContent + " div.box-index:eq(" + (_id - 1) + ")").css("display", "block");
		}
		
	//});
}
