var allways = {
	init: function() {
		$('a.sitemap-toggle').click(function() {
			text = $(this).text();
			$(this).text(((text[0] == '+') ? '-' : '+') + text.substr(1));
			if($('#sitemap-toggle').is(':visible')) {
				$('html, body').animate({scrollTop: 0}, 'slow');
				$('#sitemap-toggle').animate({ height: 'toggle', opacity: 'toggle' }, 'fast');
			}
			else
				$('#sitemap-toggle').animate({ height: 'toggle', opacity: 'toggle' }, "fast", "", function() {
					$('html, body').animate({scrollTop: $(document).height() -  $(window).height()}, 'slow');
				});
	    	return false;
		});
	}
}

var home = {
	    init: function() {
			$("div#mygalone").slideView();
			// timer pour slide
			home.timer = window.setInterval("home.slide()", 6000);
		},
		slide: function() {
			if((home.next_slide = $("#stripTransmitter0 li a.current").parent().next().find("a")) && (home.next_slide.text() == "")) home.next_slide = $("#stripTransmitter0 li:eq(1) a");
			home.next_slide.click();
		}
}

var cart = {
	clickvalid: 1,
	init: function() {
		$(".select").change(function(){
			
			var a_line = $(this).attr('id').split('_');
			var postdata = "line="+a_line[1]+"&qty="+$(this).val();
			$.post(cart.posturl_addqty, postdata, function (data, textStatus) {
	            if (textStatus == "success") {
	            	window.location = cart.url;
	            }
	        });
		});
		
		$("a.valider").click(function() {
			if(!$(".condition-panier-checkbox input:checkbox:checked").val()){
				$(".condition-panier").fadeOut(50).fadeIn(500);
				cart.clickvalid = 1;
				
				return false;
			}
			// check only one valid click
			if(--cart.clickvalid < 0) return false;
		});
		
		$('a.view-cartline-attribute').click(this.toggleAttribute);
		
		// code promo
		$('.table-contentcode').next().find('input').click(function() {
			$(this).attr('value', '');
		}).hide();
		$('.table-contentcode a').click(function(){
			$(this).toggleClass('clicked noclicked');
			
			cible = $(this).parent().parent().next().find('input');
			cible.toggle();
			
			return false;
		});
		
		$('#code_submit_btn').click(function(){
			postdata = $("input#code").serializeArray();
			$.post(cart.posturl_code, postdata, function (data, status) {
	            if (status == "success") {
	            	// just refresh if success
	            	if(data.status == 1) {
//	            		setTimeout("cart.refresh()", 1000);
	            		cart.refresh();
	            	} else if(data.status == 2)
	            	{
	            		// to log page
	            		window.location = cart.url_login;
	            	} else $("#code_return").hide().text(data.message).fadeIn(5000).fadeOut(1000);
	            }
			});
		});
		// push code promo
		if(cart.promocode) {
			$('.table-contentcode a').click();
			$('.table-contentcode').next().find('input').attr('value', cart.promocode);
			$('#code_submit_btn').click();
		}
		
	},
	refresh: function() {
		window.location = window.location.href;
	},
	toggleAttribute: function() {
		var text = $(this).text(); 
		$(this).next().animate({ height: 'toggle', opacity: 'toggle' }, "slow");
		$(this).find('b').text(((text[0] == '+') ? '-' : '+') + text.substr(1));
		
		return false;
	}
}

var sendToFriend = {
    submit: function() {
        var formdata = $('.sendtofriend-form-field').serializeArray();
	    // post the data via AJAX post
	    $.post($('#sendToFriendForm').attr('action'), formdata, function (data, textStatus) {
            if (data == 'error') {
                $('#error-bloc').show(); 
            } else {
                $('#lightbox-form').hide();
                $('#lightbox-success').show();
            }
	    });
    }
}

