function shop_tabs(id, start){
	
	//var a = $('#tabs li,a');
	var a = $('#'+id+' li a');
	var s = $('#shops');
	var ss = s.children();
	var h = ss.height()+'px';
	//var active = {'background-color': '#fff', 'color': '#990000'};
	//var inactive = {'background-color': '', 'color': '#666666'};
	
	var current = start;
	//$('#tabs li:nth-child('+current+') a').css(active)
	$('#'+id+' li:nth-child('+current+') a').attr("class", "page-left-active")
	$('.page-right').css("height", h);
	ss.each(function(i){
		if(i != current-1){
			$(this).css('display', 'none')
		}
	});
	a.click(function (aa) {
        aa.preventDefault();
		$('#shops li#shop'+current).css("display", "none");
		current = $(this).parent().index()+1;
		setTimeout( function () {
			
			panTo(current);
		}, 100);
		$('#shops li#shop'+current).css("display", "block");
		//a.css(inactive);
		//$(this).css(active);
		a.attr("class", "page-left-inactive");
		$(this).attr("class", "page-left-active");
		//aa.target.id = "ehe"
	})
	s.css('display', 'block');
}
function changeImg(targ){
/*	
	$('#mainImg').fadeTo(50, 0, function(){
		$('#mainImg').fadeTo(300, 1);
		$('#mainImg').attr("src", targ);
	});*/
	$('#mainImg').attr("src", targ);
}
function vertical_align(parent, targ){
	$(parent).each(function(i){
		var h = parseInt(($(this).height()-$('p', this).height())/2);
		$('p', this).css("padding-top", h);
	});
	
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function submitContacts(){
	var err = false;
	var wImg;
	$('.right li img').remove();
	$.params = {
		fname: [ $('[name="fname"]').val(), 'Моля, попълнете Име'],
		email1: [ $('[name="email1"]').val(), 'Моля попълнете валиден E-mail адрес'],
		message: [ $('[name="message"]').val(), 'Моля, напишете Вашият въпрос']
	}

	$.each($.params, function(index, value) {
		if(value[0] == ''){
			
			wImg = '<img rel="warning" src="templates/site/images/warningIcon.png" align="absmiddle" class="warningImg" title="'+value[1]+'" />';
			$('[name="'+index+'"]').parent().append(wImg);
			err = true;
			return;
		}
		if(index == 'email1'){
			if( !isValidEmailAddress( value[0] ) ) {
				wImg = '<img rel="warning" src="templates/site/images/warningIcon.png" align="absmiddle" class="warningImg" title="'+value[1]+'" />';
				$('[name="'+index+'"]').parent().append(wImg);
				err = true;
			}
		}
	});
	
	$('[rel="warning"]').tipsy({gravity:'w', fade: true});
	
	if(!err){
		$.params['sex'] = $('[name="sex"]').val();
		//console.log($.params);
		$.ajax({
			url: "index.php?menu=contacts",
			type: "POST",
			data: $.params,
			cache: false,
			crossDomain: true,
			dataType: "json",
			success: function (data) {
				if(data.success){
					//$('#thanks').html("Your message was send successfully.");
					alert("Запитването Ви е изпратено успешно.");
				}
				$('body').append(data);
			}
		});
	}
}
var open = false, bOpen = 135, bClose = 20, prodNum = 0, ids = [];
function bottom_line(){
	ids = [];
	var a = arguments[0];
	
	$('#bline').css('display', 'none');
	if(a.length){
		for(var i=0; i<a.length; i++){
			addProd(a[i], true, true);
		}
	}
	$('#bline h1 a').click(function(){
		open_bottom();
	});
}
function addProd(obj, closed, update){
	var a = obj;
	
	var check = false;
	if(ids.length){
		for(var i=0; i<ids.length; i++){
			if(ids[i]['id'] == a.id){
				check = false;
				break;
			} else {
				check = true;
			}
		}
	} else {
		check = true;
	}
	if($('#bline ul').children().length<6){
		if(!check){
			addStatus('този продукт вече е добавен за сравнение!');
		} else {
			ids.push(a);
			prodNum++;
			addStatus($('#bline ul').children().length);
			//$('#bline ul').append('<li><img src='+a.pic+' /><br/><div>'+a.name+'</div></li>');
			$('#bline ul div.buts').before('<li><div class="compThumbs" style="background-image:url('+a.pic+')"><a href="?menu=view-product&id='+a.id+'"><img src="templates/site/images/blank.gif" border="0" /></a></div><div>'+a.name+'</div><div class="closer" id="c'+a.id+'">x</div></li>');
			$('#bline ul li div#c'+a.id).click(function(){bottomRemove($(this))});
			//$('#bline ul').insertBefore($('#bline ul div.buts'));
			if(!update) submitProd();
		}
		
		
	} else {
		addStatus('можете да сравнявате не повече от 5 продукта!');
	}
	if(closed) {
		open_bottom(false, true);
	} else {
		open_bottom(true);
	}
	$('#bline').css('display', 'block');
	
}

function addStatus(what){
	$('#bline h1 span span').remove();
	$('#bline h1 span').append('<span style="color:#990000">( '+what+' )</span>');
}
function open_bottom(what, anim, cc){
	open = (what != undefined) ? what : !open;
	var newH = open ? bOpen : (cc)? 0 : bClose;
	if(!anim){
		$('#bline').animate({height:newH}, {duration:300, complete:(cc) ? function(){$('#bline').css('display', 'none');} : undefined})
		//$('#bline').css('display', 'block');
	} else {
		//$('#bline').css('height', newH+'px');
		$('#bline').animate({height:newH}, {duration:1, complete:function(){}});
		//$('#bline').animate({height:newH}, {duration:300, complete:(cc) ? function(){$('#bline').css('display', 'none');} : undefined})
	}
	if($('#bline ul').children().length && !open){
		addStatus($('#bline ul').children().length-1);
	}
	
}
function bottomRemove(e){
	var curId = e.attr('id').toString().substring(1);
	var ind;
	$.each(ids, function(index, value) { 
		if(curId == value['id']) ind = index;
	});
	ids.splice(ind,1);
	
	e.parent().remove();
	prodNum--;
	addStatus(prodNum);
	if(prodNum<1){
		bottomRemoveAll();
	} else {
		submitProd();
	}
}
function bottomRemoveAll(){
	$('#bline ul li').remove();
	open_bottom(false, false, true);
	prodNum = 0;
	addStatus('');
	ids = [];
	submitProd();
}
function submitProd(){
	$.get("updateSession.php", {'action': 'updateCompare', 'ids': ids}, function(theResponse){
		//console.log(theResponse);
	});
}
function showHighslide(el){
	var hWidth = parseInt(ids.length)*209;
	return hs.htmlExpand( el, {objectType:'iframe', outlineType:'rounded-white', wrapperClassName:'highslide-wrapper drag-header',         outlineWhileAnimating:true, preserveContent:false, width: hWidth } )
}
