﻿function setLocation(l)
{
	window.location = l;
}

function windowOpenBlank(url)
{
	wnd = window.open(url,"_blank");
	if (wnd.focus) {wnd.focus()}
}

function windowOpen(url,name,left,top,width,height)
{
	if (height<0)
	{
		height = window.innerHeight+height;
		height = height<300 ? 300 : height;
	}
	wnd = window.open(url,name,'left='+left+',top='+top+',width='+width+',height='+height+',resizable=yes,scrollbars=no,location=no,fullscreen=yes');
	if (wnd.focus) {wnd.focus()}
}

function windowOpenFull(url,name) 
{
	params  = 'width='+screen.width;
	params += ', height='+screen.height;
	params += ', top=0, left=0'
	params += ', fullscreen=yes';

	wnd = window.open(url,name, params);
	if (wnd.focus) {wnd.focus()}
}


//whole credits are devoted to orignal author
//
//Use: Call function goto_top()
var goto_top_type = -1;
var goto_top_itv = 0;

function goto_top_timer() 
{
	var y = goto_top_type == 1 ? document.documentElement.scrollTop : document.body.scrollTop;
	//alert(y);
	var moveby = 15; // set this to control scroll seed. minimum is fast

	y -= Math.ceil(y * moveby / 100);
	if (y < 0)
		y = 0;

	if (goto_top_type == 1)
		document.documentElement.scrollTop = y;
	else
		document.body.scrollTop = y;

	if (y == 0) {
		clearInterval(goto_top_itv);
		goto_top_itv = 0;
	}
}

function goto_top() {
	if (goto_top_itv == 0) {
		if (document.documentElement && document.documentElement.scrollTop)
			goto_top_type = 1;
		else if (document.body && document.body.scrollTop)
			goto_top_type = 2;
		else
			goto_top_type = 0;

		if (goto_top_type > 0)
			goto_top_itv = setInterval('goto_top_timer()', 25);
	}
}
/*
	FUNKCJA DO ZAPISYWANIA CIASTECZEK
	PARAMETRY: NAZWA CIASTECZKA, WARTOSC CIASTECZKA, ILOSC DNI DO WYGASNIECIA
*/
function set_cookie(cookie_name, cookie_value, cookie_days) {
	var date = new Date();
	date.setDate(date.getDate + cookie_days);
	document.cookie = cookie_name + '=' + escape(cookie_value) + ((cookie_days == null) ? '' : ';expires=' + date.toUTCString());
}

/*
	FUNKCJA POBIERA WARTOSC CISATECZKA
	PARAMETRY: NAZWA CIASTECZKA
*/
function get_cookie(cookie_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(cookie_name + "=");
			if (c_start != -1) {
				c_start = c_start + cookie_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 clear_filters() {
	set_cookie('selected_opt', null, 1);	
}

var filter_category_option = new Array();

function MyLoad()
{
	swfobject.embedSWF('/page/flash/logo.swf','flash_logo','193','125','9.0.0','',null,{menu:'false',allowScriptAccess:'sameDomain'});
	
	
	$('a[rel=external]').attr('target','_blank');
	
	/*$('#collection_menu a').click(function(){
		$('#collection_menu a[rel!='+this.rel+']').removeAttr('class');
		$(this).attr('class','active');
		$('#collection_option .sub li[class!=collection_'+this.rel+']').hide();
		$('#collection_option .sub li[class=collection_'+this.rel+']').show();
		//return false;
	});

	$('#collection_menu a.active').click();*/
	
	/* filtr menu skocz do */
	$('#filter_category').change(function(){
		$('#filter_category option:selected').each(function()
		{
			$('#product_filter_shadow').fadeIn(600);

			var collectionId = 0;
			$('#filter_collection option:selected').each(function(){
				collectionId = $(this).val();
			});
			var sUrl = 'http://'+window.location.hostname+'/getproducts/'+collectionId+'/'+$(this).val()+'.html';

			/* pobiera i usatiwa liste produktow na podstawie kolekcja.id-kategoria.id */
			jQuery.get(
				sUrl,
				null,
				function(oXML){
					var aNode = oXML.getElementsByTagName('product');
					var container = document.getElementById('filter_product');
					while(container.firstChild!=null)
						container.removeChild(container.firstChild);
					var oOption = document.createElement('OPTION');
					oOption.appendChild(document.createTextNode('Wybierz'));
					container.appendChild(oOption);
					for(var i=0;i<aNode.length;i++)
					{
						var oOption = document.createElement('OPTION');
						oOption.appendChild(document.createTextNode(aNode[i].getAttribute('title')));
						oOption.setAttribute('value',aNode[i].getAttribute('url'));
						container.appendChild(oOption);
					};
					$('#product_filter_shadow').fadeOut(600);
					return false;
				},
				'xml');
		});
		$(this).blur();
		return false;
	});

	$('#filter_product').change(function()
	{
		$('#filter_product option:selected').each(function()
		{
			var sUrl = $(this).val();
			if(sUrl.length>2)
				window.location.href = sUrl;
			return false;
		});
	});

	$('#filter_collection').change(function(){
		$('#filter_collection option:selected').each(function()
		{
			$('#product_filter_shadow').fadeIn(600);
			/* first - w momecie przeladowania strony ustawia option.selected na pierwszy element */
			while(filter_category_option.length>0)
				$(filter_category_option.shift()).appendTo('#filter_category');

			var first = true;
			$('#filter_category option[class!=collection_'+$(this).val()+']').each(function(){
				$(this).attr('disabled','disabled');
				$(this).removeAttr('selected');
				$(this).hide();
				filter_category_option.push($(this).remove());
			});
			$('#filter_category option:selected').each(function(){
				first = false;
			});
			$('#filter_category option[class=collection_'+$(this).val()+']').each(function(){
				$(this).removeAttr('disabled');
				$(this).show();
				if(first)
				{
					$(this).attr('selected','selected');
					$(this).select();
					first=false;
				}
			});
			$('#filter_category').trigger('change');
			return false;
		});
		$(this).blur();
		return false;
	});
	$('#filter_collection').trigger('change');

	$(document).ready(function () {
		$(".lightbox").lightbox();
	});

	/* FAQ */
	var iFaqVisible=0;
	$('#faq_ul .visible').each(function()
	{
		iFaqVisible++;
		$('.li_content',this).hide();
		$(this).removeClass('visible');
	});
	if(iFaqVisible>1)
		$('#faq_ul .faq_name').click(function()
		{
			$(this).parent().each(function()
			{
				if($(this).hasClass('visible'))
					$('.li_content',this).slideUp(600);
				else
					$('.li_content',this).slideDown(600);
				$(this).toggleClass('visible');
			});
		});
	else
		$('#faq_ul .li_content').show();
	$('#faq_ul .slideup').click(function()
	{
		$('.faq_name',$(this).parent().parent()).trigger('click');
	});
};
