﻿function swittch(type) {
	$('#attrib').removeClass('act');
	$('#comment').removeClass('act');
	$('#descc').removeClass('act');
	$('#ship_form_link').removeClass('act');
	$('#descrp').css('display','none');
	$('#techspec').css('display','none');
	$('#comments').css('display','none');
	$('#ship_form').css('display','none');
	if(type == 'desc') {
		$('#descc').addClass('act');
		$('#descrp').css('display','block');
		$.setCookie('descact','desc',{duration: 1,path:'/'});
	} else if(type == 'attr') {
		$('#attrib').addClass('act')
		$('#techspec').css('display','block');
		$.setCookie('descact','attr',{duration: 1,path:'/'});
	} else if(type == 'comment') {
		$('#comment').addClass('act')
		$('#comments').css('display','block');
		$.setCookie('descact','comment',{duration: 1,path:'/'});
	}
	else if(type == 'ship_form') {
		$('#ship_form_link').addClass('act')
		$('#ship_form').css('display','block');
		$.setCookie('descact','ship_form',{duration: 1,path:'/'});
	}
}

function toBasket(id) {
	var dataString = $('#tobasket'+id).serialize();
	
	jQuery.ajax({
		type: "POST",
		url: "/index.php?",
		data: dataString,
		
		success: function(data) {
			var variant = $('#variant'+id);
			var height = variant.height();
			var width = variant.width();
			var top = variant.offset().top;
			var left = variant.offset().left;
			
			var topcart = $('#top_cart');
			var newHeight = topcart.innerHeight();
			var newWidth = topcart.innerWidth();
			var newTop = topcart.offset().top;
			var newLeft = topcart.offset().left;
			
			var dh = '<div style="position: absolute; border: 2px solid #c1c1c1;" id="outline"></div>';
			$('#tobasket'+id).append(dh);
			
			$('#outline').css('height',height);
			$('#outline').css('width',width-4);
			$('#outline').css('top',top-6);
			$('#outline').css('left',left-134);
			
			$('#outline').animate (
				{
					"width": newWidth,
					"height": newHeight,
					"top":newTop-2,
					"left":newLeft-134
				},
				500,
				function() {
					$('#outline').remove();
				}
			);
			
			jQuery('#top_cart').html(data);
		},
		
		error: function(r, status, err) {
			console.log(status);
		}
	});
}
var ajProcess = false;
function toCompare(id,fid,repl) {
	if(!ajProcess) {
		ajProcess = true;
		if(repl == null)
			repl = false;
		var dataString = $('#'+fid).serialize();
		if($('#hidd_comp .has').size() < 4 || $('#'+fid+' input[name="removecompare"]').val() == '1') {
			jQuery.ajax({
				type: "POST",
				url: "/index.php",
				data: dataString,
				
				success: function(data) {
					var was = $('#hidd_comp .has').size();
					$('#hidd_comp').html(data);
					
					if($('#'+fid+' input[name="addcompare"]').val() == '1' ) {
						if(repl) {
							$('#'+fid+' input[name="addcompare"]').attr('name','removecompare');
							$('#'+fid+' a[class="compare"]').html('Iš palyginimo');
						} else {
							$('#cmpform'+id).hide();
						}
					} else {
						if(repl) {
							$('#'+fid+' input[name="removecompare"]').attr('name','addcompare');
							$('#'+fid+' a[class="compare"]').html('Palyginti');
						} else {
							$('#cmpform'+id).show();
						}
					}

					if($('#hidd_comp .has').size() > 0) {
						$('#compButt').attr('src','/out/baracuda/img/top_items_icon_2_2.png');
						if(was < $('#hidd_comp .has').size() || was > $('#hidd_comp .has').size()) {
							$('#compButt').fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
						}
					} else {
						$('#compButt').attr('src','/out/baracuda/img/top_items_icon_2.png');
					}
					ajProcess = false;
				},
					
				error: function(r, status, err) {
					ajProcess = false;
				}
			});
		} else {		
			$('#compButt').trigger('mouseenter');
			ajProcess = false;
		}
	}
}

function doLogin() {
	var dataString = $('#ajLogin').serialize();
	
	jQuery.ajax({
		type: "POST",
		url: "/index.php?",
		data: dataString,
		
		success: function(data) {
			if(data == 'wronglogin') {
				if($('#hle')) $('#hle').remove();
				$('#tophlt').after('<p id="hle" style="display: inline-block; color:red; font-weight: bold; margin-bottom: 3px;">Neteisingi prisijungimo duomenys!</p>');
				setTimeout(function() {
					$('#hle').fadeOut(1500,function() {$('#hle').remove()});
				},5500);
			} else {
				$('#logedarea').html(data);
				$('#hidden_login').fadeOut(200);
			}
		},
		
		error: function(r, status, err) {
			console.log(status);
		}
	});
}

function reportError(msg) {
	alert(msg);
}

function contactsMap() {
	window.open('/googlemaps.html','name','height=600,width=700');
}

var timeout;
var timeoutt;
var oldsearch;
var logtime;
$(document).ready(function() {
	$('#compButt').bind("mouseenter",function() {
		clearTimeout(timeoutt);
		$('.ccompare').fadeIn(500);
	});
	$('#compButt').bind("mouseleave",function() {
			timeoutt = setTimeout(function() {
				$('.ccompare').fadeOut(500);
			},1500);
	});
	$('.ccompare').bind("mouseenter",function() {
		clearTimeout(timeoutt);
	});
	$('.ccompare').bind("mouseleave",function() {
		timeoutt = setTimeout(function() {
				$('.ccompare').fadeOut(500);
			},1500);
	});
	$('#prkt').bind("click", function(e) {
		clearTimeout(timeout);
		$('.drop').fadeIn(100);
	});
	$('#nav').bind("mouseleave", function() {
		timeout = setTimeout(function() {
			$('.drop').fadeOut(100);
		},1500);
	});
	$('#searchparam').bind('focus',function() {
		oldsearch = $('#searchparam').val();
		$('#searchparam').val('');
	});
	$('#searchparam').bind('blur',function() {
		var newval = $('#searchparam').val();
		if(newval == '') {
			$('#searchparam').val(oldsearch);
		}
	});
	
	$('#hidLog').click(function(e){
		e.preventDefault();
		$('#hidden_register').fadeOut(20);
		$('#hidden_login').fadeIn(200);
	});
	
	$('#hidden_login').bind("mouseleave",function() {
		logtime = setTimeout(function() {
			$('#hidden_login').fadeOut(200);
		},1500);
	});
	$('#hidden_login').bind("mouseenter",function() {
		clearTimeout(logtime);
	});
	
	$('#hidReg').click(function(e){
		e.preventDefault();
		$('#hidden_login').fadeOut(20);
		$('#hidden_register').fadeIn(200);
	});
	
	$('#test_Login').click(function(e){
		
	})
	
	$('#hidden_register').bind("mouseleave",function() {
		logtime = setTimeout(function() {
			$('#hidden_register').fadeOut(200);
		},1500);
	});
	$('#hidden_register').bind("mouseenter",function() {
		clearTimeout(logtime);
	});
	
});
