var ajaxFilterUpdate;
var ajaxContentUpdate;
var ajaxNavibarUpdate;
var lastContent = "";
var canClick = true;
var spcOpen = false;

function jsRefreshNavibar()
{
	if (!isdefined2('isCatalog') || !isCatalog)
	{
		location.href = "/store/tees/";
		return true;
	}
	fade('shopnavibar');
	fade('shopnavibar2');
	ajaxNavibarUpdate = ajax_create_request();
	ajax_make_get_request(ajaxNavibarUpdate, '/store/ajax.html?p=shirts&s=navibar', jsCallbackNavibarContentUpdateChange);
	canClick = false;
}

function jsRefreshContent()
{
	if (!isdefined2('isCatalog') || !isCatalog)
	{
		location.href = "/store/tees/";
		return true;
	}
	jsSetLoaderToDiv(document.getElementById('shopcontent'), 32, 32, "načítava sa /loading...");
	ajaxContentUpdate = ajax_create_request();
	ajax_make_get_request(ajaxContentUpdate, '/store/ajax.html?p=shirts&s=content', jsCallbackContentUpdateChange);
	canClick = false;
}

function jsChangePage(pg)
{
	if (!canClick)
		return false;
		
	ajaxFilterUpdate = ajax_create_request();
	ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&v=y&y=' + pg, jsCallbackNavibarUpdateChange);
	canClick = false;
}

function jsSetPPV(ppv)
{
	if (!canClick)
		return false;
		
	ajaxFilterUpdate = ajax_create_request();
	ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&v=x&x=' + ppv, jsCallbackNavibarUpdateChange);
	canClick = false;
}

function jsSetGenderFilter(gender, onlyicon)
{
	if (!canClick)
		return false;
		
	if (gender=='boys')
	{
		var shopdiv = document.getElementById('shopcontent');
		document.getElementById('img_gender_boys').src = "/pub/img/tricka-filter_boys_hit.jpg";
		document.getElementById('img_gender_girls').src = "/pub/img/tricka-filter_girls.jpg";
		
		if (!onlyicon) ajaxFilterUpdate = ajax_create_request();
		if (!onlyicon) ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&v=g&g=boys', jsCallbackFilterUpdateChange);
	} else if (gender=='girls')
	{
		var shopdiv = document.getElementById('shopcontent');
		document.getElementById('img_gender_boys').src = "/pub/img/tricka-filter_boys.jpg";
		document.getElementById('img_gender_girls').src = "/pub/img/tricka-filter_girls_hit.jpg";
		
		if (!onlyicon) ajaxFilterUpdate = ajax_create_request();
		if (!onlyicon) ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&v=g&g=girls', jsCallbackFilterUpdateChange);
	}
	
	if (!onlyicon) canClick = false;
}

function jsSetCutFilter(cut, onlyicon)
{
	if (!canClick)
		return false;
	
	var shopdiv = document.getElementById('shopcontent');
	document.getElementById('img_cut_tshirt').src = "/pub/img/tricka-filter_tricka.jpg";
	document.getElementById('img_cut_shirt').src = "/pub/img/tricka-filter_dlhe.jpg";
	document.getElementById('img_cut_undershirt').src = "/pub/img/tricka-filter_tielka.jpg";
	
	if (cut=='tshirt')
	{
		document.getElementById('img_cut_tshirt').src = "/pub/img/tricka-filter_tricka_hit.jpg";
		
		if (!onlyicon) ajaxFilterUpdate = ajax_create_request();
		if (!onlyicon) ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&v=c&c=' + cut, jsCallbackFilterUpdateChange);
	} else if (cut=='shirt')
	{
		document.getElementById('img_cut_shirt').src = "/pub/img/tricka-filter_dlhe_hit.jpg";
		
		if (!onlyicon) ajaxFilterUpdate = ajax_create_request();
		if (!onlyicon) ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&v=c&c=' + cut, jsCallbackFilterUpdateChange);
	} else if (cut=='undershirt')
	{
		document.getElementById('img_cut_undershirt').src = "/pub/img/tricka-filter_tielka_hit.jpg";
		
		if (!onlyicon) ajaxFilterUpdate = ajax_create_request();
		if (!onlyicon) ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&v=c&c=' + cut, jsCallbackFilterUpdateChange);
	}
	
	if (!onlyicon) canClick = false;
}

function jsSetSizeFilter(size)
{
  if (!canClick)
    return false;
  
  ajaxFilterUpdate = ajax_create_request();
  ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&v=z&z=' + size, jsCallbackFilterUpdateChange);
  
  canClick = false;
}

function jsResetFilter()
{
  if (!canClick)
		return false;
	
	document.getElementById('img_cut_tshirt').src = "/pub/img/tricka-filter_tricka.jpg";
	document.getElementById('img_cut_shirt').src = "/pub/img/tricka-filter_dlhe.jpg";
	document.getElementById('img_cut_undershirt').src = "/pub/img/tricka-filter_tielka.jpg";
	document.getElementById('img_gender_boys').src = "/pub/img/tricka-filter_boys.jpg";
	document.getElementById('img_gender_girls').src = "/pub/img/tricka-filter_girls.jpg";
	document.getElementById('filter_size').value = 0;
	document.getElementById('spc_current').src = '/pub/img/tricka-filter_spektrum-all.jpg';
	
  var shopdiv = document.getElementById('shopcontent');
  
  ajaxFilterUpdate = ajax_create_request();
  ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&f', jsCallbackFilterUpdateChange);
  canClick = false;
}

function jsCallbackFilterUpdateChange()
{
	if (ajaxFilterUpdate.readyState == 4)
	{
		if (ajaxFilterUpdate.responseText == "1")
		{
			jsRefreshNavibar()
			jsRefreshContent();
		}
		canClick = true;
	}
}

function jsCallbackNavibarUpdateChange()
{
	if (ajaxFilterUpdate.readyState == 4)
	{
		if (ajaxFilterUpdate.responseText == "1")
		{
			jsRefreshNavibar();
			jsRefreshContent();
		}
		canClick = true;
	}
}

function jsCallbackNavibarContentUpdateChange()
{
  if (ajaxNavibarUpdate.readyState == 4)
  {
	fade('shopnavibar');
	fade('shopnavibar2');
    document.getElementById('shopnavibar').innerHTML = ajaxNavibarUpdate.responseText;
	document.getElementById('shopnavibar2').innerHTML = ajaxNavibarUpdate.responseText;
    canClick = true;
  }
}

function jsCallbackContentUpdateChange()
{
  if (ajaxContentUpdate.readyState == 4)
  {	
  	lastContent = ajaxContentUpdate.responseText;
 	fade('shopcontent');
	setTimeout("jsSetContent();", 500);
    canClick = true;
  }
}

function jsSetContent()
{
	fade('shopcontent');
    document.getElementById('shopcontent').innerHTML = lastContent;
	lastContent = "";
}

function jsRotateBigImage()
{
	var next = current_bigimg * 1;
	next++;
	if (!bigimgs[next])
	{
		next = 0;
	}
	
	fade('downimage');
	setTimeout("jsUpdateBigImage('" + next +"');", 700);
}

function jsUpdateBigImage(next)
{
	var div = document.getElementById('downimage');
	fade('downimage');
	div.innerHTML = "<a href=\"javascript:void(0);\" onclick=\"jsRotateBigImage();\"><img src=\"/pub/img/shirt/" + bigimgs[next] + "\" alt=\"veľký náhľad\" /></a>";
	current_bigimg = next;	
}

function jsSpcOpen()
{
	var div = document.getElementById('spc_selector');
	
	if (!spcOpen)
	{
		div.style.visibility = 'visible';
		div.style.opacity = '0';
		fade('spc_selector');
		spcOpen = true;
	} else
	{
		fade('spc_selector');
		spcOpen = false;
		setTimeout('jsSpcHide()', 1000);
	}
}

function jsSpcHide()
{
	var div = document.getElementById('spc_selector');
	div.style.visibility = 'hidden';
	div.style.opacity = '0';
}

function jsSpcSelect(spectrum)
{
	if (!canClick)
		return false;
	
	canClick = false;
	ajaxFilterUpdate = ajax_create_request();
	ajax_make_get_request(ajaxFilterUpdate, '/store/ajax.html?p=shirts&s=filter&v=k&k=' + spectrum, jsCallbackFilterUpdateChange);
	document.getElementById('spc_current').src = '/pub/img/tricka-filter_spektrum-' + spectrum + '.jpg';
	jsSpcOpen();
}
