$(document).ready(function(){
	$('.gallery li div').addClass('popups');
	$('.gallery h2').click(function() {	
		wrap = $(this).next();
		if($(this).hasClass('clicked')){
			wrap.animate({ 
			        height: '380px'
			      }, 500 );
			$(this).removeClass('clicked');
			//$('.showless,.showmore').toggle();
			$(this).children('.showless').toggle();
			$(this).children('.showmore').toggle();
			$(this).parent().children('.shad').show();
			wrap.removeClass('opened').addClass('closed');
		}
		else{
			$(this).addClass('clicked');
			he = wrap.height();
			wrap.css({height:he});
				$(this).children('.showless').toggle();
				$(this).children('.showmore').toggle();
				$(this).parent().children('.shad').hide();
			wrap.removeClass('closed').addClass('opened').slideDown();
			he1 = wrap.children('ul').height();
			wrap.animate({ 
			        height: he1
			      }, 500 );
		}
		
		});
		
		function checkcart(b){
				if(($('.data tr.items').length+b)>0){
					$('.cart').addClass('notempty');
					//calculate price
					sum = 0;
					$('.itemprice').each(function(index){
						aa = $(this).attr('id');
						num = $("."+aa+" input:checked").length;
						num=(num==0)?1:num;
					 	sum =  parseInt($(this).text())*num + sum;
					});
					$('#itogprice').text(sum);
					$('#itogpricesum').attr("value",sum);
				}
				else{
					$('.cart').removeClass('notempty');
				}
			}
		
		$('.gallery li i,.gallery li a').click(function() {	
			lili = $(this).closest('li');
			popup = lili.children('div.popups');
			popup.html('<img src="'+ lili.children('a').attr('href') +'" /><span></span>');
			$('.gallery li div').fadeOut();
			popup.fadeIn();
			return false;
		});
		$('.gallery li div').click(function() {	
			$(this).fadeOut();
			
			});
		$('.data .remove').live("click", function(){	
			$(this).closest('tr').fadeOut(500, function(){$(this).closest('tr').remove();checkcart(0);});
			
			return false;
			});
			
			a=0;
			
			
		$('.gallery li .buy').click(function() {	
			lili = $(this).closest('li');
			idid = lili.attr('id');
			title = $(this).closest('li').children('a').text();
			price = $(this).attr('title');
			var tinyURL = 'http://armour.genn.org/img/o_O/';
			table = $('.data tbody');
			lili.children('.message').remove();
			if($('.cart #item'+idid).length>0){
				//alert('uje est!');
				lili.append('<div class="message">Already in your cart</div>');
				lili.children('.message').fadeIn(700,function(){lili.children('.message').fadeOut('1500');});
			}
			else{
				lili.append('<div class="message">It is in your shopping cart</div>');
				lili.children('.message').fadeIn(700,function(){lili.children('.message').fadeOut('1500');});
			$(".data tr:last").after('<tr class="items" id="item'+idid+'"><th><img src="'+tinyURL+idid+'.jpg" alt="" /><input type="hidden" value="'+tinyURL+idid+'.jpg" name="tinypic['+idid+']"  /></th><td><h3>'+title+' &#8212; <b class="itemprice" id="ul'+idid+'">'+price+'</b></h3><input type="hidden" value="'+price+'" name="price['+idid+']"  /><input type="hidden" name="title['+idid+']" value="' + title +' - '+ price +'" /><ul class="ul'+idid+'"><li><input value="XS" name="'+idid+'[]" type="checkbox" id="'+idid+'sizeXS"><label for="'+idid+'sizeXS">XS</label></li><li><input value="S" name="'+idid+'[]" type="checkbox" id="'+idid+'sizeS"><label for="'+idid+'sizeS">S</label></li><li><input value="M"  name="'+idid+'[]" type="checkbox" checked="checked" id="'+idid+'sizeM"><label class="active" for="'+idid+'sizeM">M</label></li><li><input value="L" name="'+idid+'[]" type="checkbox" id="'+idid+'sizeL"><label for="'+idid+'sizeL">L</label></li><li><input value="XL" name="'+idid+'[]" type="checkbox" id="'+idid+'sizeXL"><label for="'+idid+'sizeXL">XL</label></li></ul><a class="remove" href="#">Remove</a></td></tr>'); 
			 clonedRow = $(".data tr:last");
			
			if(a==0){
				$.scrollTo( '.data', 500, {onAfter:function(){clonedRow.effect("highlight", {}, 1000); } });
				a=1;
				}
				checkcart(0);
			}
			return false;
		});
		
$('.buyitall').click(function() {
	$('#cart').submit();
	return false;
	});
	
		$('.cart li input').live("click", function(){	
			
			$(this).next().toggleClass('active');
			checkcart(0);
		});
		function checkemail(){
			email = $('#email').attr('value');
			isvalid =  /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email);
			if(isvalid){
				
				return true;
				}
			else{
				//alert('wrong email');
				$('#email').effect("highlight", {
						color:'#ff6600'
				}, 2000);
				return false
				}
			}
		function hideform(){
			//alert('sent');
			$('#cart .items').remove();
			$('.nothing td').text('All your tshirts are belong to you soon.')
			checkcart(0);
			}
		var options = { 
		        beforeSubmit:  checkemail,  // pre-submit callback 
		        success:       hideform  // post-submit callback 
		    };
		$('#cart').ajaxForm(options);
		
		$('input[title!=""],textarea[title!=""]').hint();
	
		$('.gallery li').hover(function() {
		  $(this).children('.buy').show();
		}, function() {
		  $(this).children('.buy').hide();
		});
		
		
	});
	//plugin
	jQuery.fn.hint = function () {
	  return this.each(function (){
	    var t = jQuery(this); 
	    var title = t.attr('title'); 
	    if (title) { 
	      t.blur(function (){
	        if (t.val() == '') {
	          t.val(title);
	          t.addClass('blur');
	        }
	      });
	      t.focus(function (){
	        if (t.val() == title) {
	          t.val('');
	          t.removeClass('blur');
	        }
	      });
	      t.blur();
	    }
	  });
	}

