function windowopen(a,b,c) {
	nwindow=window.open(a,b,c);
	nwindow.focus();
}

function InstallPngFix() {
	if (($.browser.msie)&&(parseInt(jQuery.browser.version) < 7)){
		$('img[src*=png], .png').each(function(){
			$(this).css('behavior', 'url(js/iepngfix.htc)');
		});
	}
}
function InstallZebra(){
	$('.zebra tr:even').addClass('even');
	$('.zebra li:even').addClass('even');
	$('.zebra tr:odd').addClass('odd');
	$('.zebra li:odd').addClass('odd');
	
	$('.sortZebra tbody tr:even').addClass('even');
	$('.sortZebra tbody tr:odd').addClass('odd');
	
	$('.sortZebra tbody tr.none').removeClass('odd');
}

function loginError(message){
	if (message.length > 0) {
		$("#open").click();
		$('#errorContainer').html(message);
		$('#errorContainer').show();
	}
}
function authorization(form)
{
	var defaultLoginValue = $('#hdnDefaultLoginValue').val();
	var defaultPasswordValue = $('#hdnDefaultPasswordValue').val();

	var loginValue = $('#txtLogin').val();
	var passwordValue = $('#txtPassword').val();


	if ( (loginValue == defaultLoginValue || !(loginValue.length > 0)) ||  (passwordValue == defaultPasswordValue || !(passwordValue.length > 0))  )
	{
		if (loginValue == defaultLoginValue || !(loginValue.length > 0))
			$('#txtLogin').css('border', '1px solid red');
		else
			$('#txtLogin').css('border', 'none');

		if (passwordValue == defaultPasswordValue || !(passwordValue.length > 0))
			$('#txtPassword').css('border', '1px solid red');
		else
			$('#txtPassword').css('border', 'none');


		loginError($('#hdnEmptyAuthirizationDataMessage').val());
		return false;
	}

	return true;
}

function focused(obj,defaultValue){
	call_focused = true;
	if (obj.attr('value') == defaultValue) {
		obj.attr('value','');
	}
}
function blured(obj,defaultValue){
	call_focused = false;
	Callback_timer();
	if (obj.attr('value').replace(/ /g,'')=='') {
		obj.attr('value',defaultValue);
	}
}

function noneScroll(){
	alert($(this).parent('div').find('ul').css('margin-left'))
}

function OnDelete(url, txt) {
	if (confirm(txt)) document.location.href = url;
}

function PlusMinusDel(){
	$('table#buy img[alt=up]').click(function(){
		$('#recalButton').removeClass('greyButton').addClass('button');
		var currentInput = $(this).prevAll('input');
		var oldValue = parseInt(currentInput.attr('value'));
		if (oldValue<999){
			currentInput.val(oldValue+1);
		}
		
	})
	$('table#buy img[alt=down]').click(function(){
		$('#recalButton').removeClass('greyButton').addClass('button');
		var currentInput = $(this).prevAll('input');
		var oldValue = parseInt(currentInput.attr('value'));
		if (!(oldValue<=1)){
			currentInput.attr('value',oldValue-1);
		}
	})
	$('table#buy img[alt=delete]').click(function(){
		var currentTr = $(this).parents('tr:first');
		var currentInput = $(currentTr).children('td.counter').children('input');
		currentInput.val(0);
		currentTr.hide();
		CartSubmit();
	})
}

function createScroll(){

		$('div.scrollRight').hover(
			function(){$(this).addClass('scrollRightHover')},
			function(){$(this).removeClass('scrollRightHover')}
		)

		$('div.scrollLeft').hover(
			function(){$(this).addClass('scrollLeftHover')},
			function(){$(this).removeClass('scrollLeftHover')}
		)

		$('div.scrollArea ul').each(function(e){
			if ($(this).find('li').length*$(this).find('li:first').width()<550){
				$(this).parent('div').prev('div.scrollRight').addClass('scrollNone')
									 .prev('div.scrollLeft').addClass('scrollNone');
			}
		})

	$('div.scrollRight').click(function(){
		var curMarg = parseInt($(this).parent('div').find('ul').css('margin-left'));
		var sizeItem =  parseInt($(this).parent('div').find('ul').find('li:first').width());
		var items = $(this).parent('div').find('ul').find('li').length;
		var seeItem =  Math.ceil((parseInt($(this).nextAll('div.scrollArea').width()))/sizeItem);

		if ((sizeItem*(items+1)+curMarg-550) >= sizeItem) {
			$(this).parent('div').find('ul').animate({
				"marginLeft": "-=" + sizeItem
			}, 10);
		}

//		$('.console').append(' ss= '+(sizeItem*items+curMarg-550)+'<br>');
		if(sizeItem*items+curMarg-550<=0){
				$(this).parent('div').find('ul').animate({
				"marginLeft": "0px"
			}, "fast");

		}
	})

	$('div.scrollLeft').click(function(){
		var curMarg = parseInt($(this).parent('div').find('ul').css('margin-left'));
		var sizeItem =  parseInt($(this).parent('div').find('ul').find('li:first').width());
		var items = $(this).parent('div').find('ul').find('li').length;
		if (curMarg < -50) {
			$(this).parent('div').find('ul').animate({
				"marginLeft": "+=" + sizeItem
			}, 10);
		}
		if ((curMarg >-50)&&(sizeItem*items>550)){
			var stophere=12;
				$(this).parent('div').find('ul').animate({
				"marginLeft": -sizeItem*items+545+"px"
			}, "fast");
		}

	})
}



function createScroll_big(){

	var width = 730;
	
		$('div.scrollRight_big').hover(
			function(){$(this).addClass('scrollRightHover_big')},
			function(){$(this).removeClass('scrollRightHover_big')}
		)

		$('div.scrollLeft_big').hover(
			function(){$(this).addClass('scrollLeftHover_big')},
			function(){$(this).removeClass('scrollLeftHover_big')}
		)

		$('div.scrollArea_big ul').each(function(e){
			if ($(this).find('li').length*$(this).find('li:first').width()<width){
				$(this).parent('div').prev('div.scrollRight_big').addClass('scrollNone_big')
									 .prev('div.scrollLeft_big').addClass('scrollNone_big');
			}
		})

	$('div.scrollRight_big').click(function(){
		var curMarg = parseInt($(this).parent('div').find('ul').css('margin-left'));
		var sizeItem =  parseInt($(this).parent('div').find('ul').find('li:first').width());
		var items = $(this).parent('div').find('ul').find('li').length;
		var seeItem =  Math.ceil((parseInt($(this).nextAll('div.scrollArea').width()))/sizeItem);

		if ((sizeItem*(items+1)+curMarg-width) >= sizeItem) {
			$(this).parent('div').find('ul').animate({
				"marginLeft": "-=" + sizeItem
			}, 10);
		}

//		$('.console').append(' ss= '+(sizeItem*items+curMarg-550)+'<br>');
		if(sizeItem*items+curMarg-width<=0){
				$(this).parent('div').find('ul').animate({
				"marginLeft": "0px"
			}, "fast");

		}
	})

	$('div.scrollLeft_big').click(function(){
		var curMarg = parseInt($(this).parent('div').find('ul').css('margin-left'));
		var sizeItem =  parseInt($(this).parent('div').find('ul').find('li:first').width());
		var items = $(this).parent('div').find('ul').find('li').length;
		if (curMarg < -50) {
			$(this).parent('div').find('ul').animate({
				"marginLeft": "+=" + sizeItem
			}, 10);
		}
		if ((curMarg >-50)&&(sizeItem*items>width)){
			var stophere=12;
				$(this).parent('div').find('ul').animate({
				"marginLeft": -sizeItem*items+width-5+"px"
			}, "fast");
		}

	})
}




function getWndSize() {
 var wnd = {width: 0, height:0};
 if( typeof( window.innerWidth ) == 'number' ) {
     //Non-IE, normal browsers
  wnd.width = window.innerWidth;
  wnd.height = window.innerHeight;
 } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
     //IE 6+ in 'standards compliant mode'
  wnd.width = document.documentElement.clientWidth;
  wnd.height = document.documentElement.clientHeight;
 } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  //IE compatible
  wnd.width = document.body.clientWidth;
  wnd.height = document.body.clientHeight;
 }
 return wnd;
}

function getDocHeight() {
    return Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );
}

function getScrollXY() {
 var scroll = {x:0, y:0};
 if( typeof( window.pageYOffset ) == 'number' ) {
  //Netscape compliant
  scroll.y = window.pageYOffset;
  scroll.x = window.pageXOffset;
 } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
  //DOM compliant
  scroll.y = document.body.scrollTop;
  scroll.x = document.body.scrollLeft;
 } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
     //IE
  scroll.y = document.documentElement.scrollTop;
  scroll.x = document.documentElement.scrollLeft;
 }
 return scroll;
}

function logoutMe() {
	document.LoginForm.event.value = 'logout';
	document.LoginForm.submit();
}

$(document).ready(function(e) {
	try {
		$('input:text, input:password, textarea, .inCenter select').addClass('inText');
		InstallPngFix();
		InstallZebra();
		PlusMinusDel();
		createScroll();
		createScroll_big();
		

		$('#changeLang').hover(
			function(){$(this).addClass('active')},
			function(){$(this).removeClass('active')}
		)

		$('.button #LoginButton').click(function(){
			loginError();
			return false;
		});

		$('#company').click(function(){
			$('#companyInfo').toggle();
		})

		// Expand Panel
		$("#open").click(function(){
			$("#LoginPanel").slideDown("normal");
		});

		// Collapse Panel
		$("#close").click(function(){
			$("#LoginPanel").slideUp("normal");
		});

		// Switch buttons from "Log In | Register" to "Close Panel" on click
		$("#toggle a").click(function () {
			$("#toggle a").toggle();
		});

		if ($('#hdnIpBlocked').val() == 'true')
		{
			loginError($('#hdnIpBlockedMessage').val());
		}
		else
		if ($('#hdnIncorrectSignIn').val() == 'true')
		{
			var message = $('#hdnIncorrectSignInMessage').val() + '<br /><br />' + $('#hdnTrySignInCountInfo').val();
			loginError(message);
		}
		else
		if ($('#hdnAuth_not_validated_user').val() == 'true')
		{
			var message = $('#hdnAuth_not_validated_user_message').val() + '<br />';
			loginError(message);
		}
		else
		if ($('#hdnAuth_denied_from').val() !== undefined){
			if ($('#hdnAuth_denied_from').val() != 'false')
			{
				var message = $('#hdnAuth_denied_message').val();
				loginError(message);
			}
		}

	} catch(e) {
//		alert(e);
	}

});

// Create a cookie with the specified name and value.
function SetCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue);
  // Expires the cookie in one month
  var date = new Date();
  date.setMonth(date.getMonth()+1);
  document.cookie += ("; expires=" + date.toUTCString()); 
}
// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  // a cookie with the requested name does not exist
  return null;
}
// Delete the cookie with the specified name.
function DelCookie(sName)
{
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function tonumber(value){
	return validn(value.trim());
}

function validn(value)
{
	if(isNaN(value)) value = 1;
	value = Math.abs(Math.round(value));
	if(value == 0) value = 1;
	if(value > 999) value = 999;
	return value;
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}


function scroll_window(){
	var scrollTop = 0;
	if($.browser.safari) scrollTop = document.getElementById('bodyid').scrollTop;
	else scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
	SetCookie('scrolltop', scrollTop);		
}

function scrollto(){	
	//alert($.browser.msie+" "+$.browser.opera+" "+$.browser.mozilla+" "+$.browser.safari+" "+$.browser.webkit);	
	var scrollTop = GetCookie('scrolltop');
	if(document.body.scrollTop){
		document.body.scrollTop = scrollTop;
	}else{
		if($.browser.safari) document.getElementById('bodyid').scrollTop = scrollTop;
		else document.documentElement.scrollTop = scrollTop;
	}		
}
window.onscroll = scroll_window;
