var config = {
	'ad_timeout': 13,
	'ad_viewed': false
}

$(document).ready(function() {

	// Petit patch permetant de redimentionner la taille du div principal pour le style
	//$('#site').css('height', ($('#contenu').outerHeight()>500?$('#contenu').outerHeight():500)+'px');
	
	// Insers 2 div en haut + en bas du div de contenu pour recevoir l'image du haut du cadre
	var contenu_div = '<div id="cadre_contenu_haut"></div>';
	contenu_div += $('#contenu').html();
	$('#contenu').html(contenu_div);
	
	// Pareil pour les petits coupons
	var div_coupon_top = '<div class="img_coupon_top"></div>';
	var div_coupon_bottom = '<div class="img_coupon_bottom"></div>';
	$(".coupon").each(function (i) {
		$(this).html(div_coupon_top+$(this).html()+div_coupon_bottom)
	});
	
	$(".coupon_s").each(function (i) {
		$(this).html('<div class="img_coupon_s_top"></div>'+$(this).html()+'<div class="img_coupon_s_bottom"></div>')
	});
	
	$('#pied').corner("round 7px");
	$('div.cases').wrap('<div class="outer_case"></div>');
	$('div.cases').corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$('div.case_cat').wrap('<div class="outer_case_cat"></div>');
	$('div.case_cat').corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$('div.sous_cat').wrap('<div class="outer_sous_cat"></div>');
	$('div.sous_cat').corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	$('div.sous_cat_select').wrap('<div class="outer_sous_cat"></div>');
	$('div.sous_cat_select').corner("round 7px").parent().css('padding', '1px').corner("round 7px");
	
	// Coutour arrondie des div
	$('div.contour').each(function () {
		$(this).wrap('<div class="outer_contour"></div>');
		$(this).corner("round 7px").parent().css('padding', '1px').corner("round 7px");
		$(this).parent().css('width', $(this).css('width'));
		//$(this).css('width', '');
		if ($(this).css('float')) {
			$(this).parent().css('float', $(this).css('float'));
		}
		$(this).css({ float: '', width: '' });
	});

	$('#lien_marchand').corner("round 7px");
	
	$("#newsletter").validate({
		rules: {
			nl_email: {
				required: true,
				email: true
			}
		},
		messages: {
			nl_email: ""
		}
	});
	
	// Lance une lightbox s'il déborde de la page
	if ($.cookie('lbx_ad')) {
		config.ad_viewed = true;
	}
	$("html").hover(
		function () {

		},
		function () {
			config.ad_timeout = setTimeout(function() {
				if (!config.ad_viewed) {
					$.fn.colorbox({
						href: '/public/lightbox/lbx.php',
						open: true,
						height: 350,
						width: 600
					});
					config.ad_viewed = true;
					
					var date = new Date();
					date.setTime(date.getTime() + (20 * 60 * 60 * 1000));
					$.cookie('lbx_ad', 1, { path: '/', expires: date });
					return false;
				}
			}, 500);
			

		}
	);
	
	// Mets les swf de copie et ouvre le popup
	$(".input_codes").each(function () {
		var clip = new ZeroClipboard.Client();
		clip.setText('');
		clip.glue($(this).attr('id'));
		var ceThis = $(this);
		
		clip.addEventListener('mouseover', function(client) {
			$(ceThis).parent().children('.copyTxt').html('<span style="background-image: url(/img/copy.gif); height: 19px; color: #222; font-weight: bold; padding: 0 15px 2px; 50px;">Cliquez pour Copier</span>');
		});
		clip.addEventListener('mouseout', function(client) {
			$(ceThis).parent().children('.copyTxt').html('');
		});
		clip.addEventListener('mouseup', function(client) {
			clip.setText($(ceThis).val());
			reveler(ceThis);
		});
	});

});


/*
// Inscription à la newsletter dans le menu
function inscriptionNewsletter() {
	if (checkEmail($('#newsletter').val())) ;
	return true;
}

// Vérifie l'email
function checkEmail(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
		return true;
	}
	return false;
}*/

// Ajout aux favorits
function ajoutFavorit() {
	if (navigator.appName != 'Microsoft Internet Explorer') 
		window.sidebar.addPanel('CodesDeReductions.com', 'http://www.codesdereductions.com', '');
	else
		window.external.AddFavorite("http://www.codesdereductions.com/","CodesDeReductions.com");
}

// Affiche le code
function reveler(ceThis) {
	$(ceThis).css({background:'#FFFFFF', color:'#000000'});
	$(ceThis).attr('onclick', '');
	win2=window.open($(ceThis).parent().children('.url_site').val(),'site','');
	win2.blur();
	window.focus();
	$(ceThis).select();
}

