/*
 * jQuery hashchange event - v1.3 - 7/21/2010
 * http://benalman.com/projects/jquery-hashchange-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);


//this parse style & remove style & rebuild style. I like the first one.. but anyway exploring..
jQuery.fn.extend
({
    removeCSS: function(cssName) {
        return this.each(function() {

            return $(this).attr('style',

            jQuery.grep($(this).attr('style').split(";"),
                    function(curCssName) {
                        if (curCssName.toUpperCase().indexOf(cssName.toUpperCase() + ':') <= 0)
                            return curCssName;
                    }).join(";"));
        });
    }
});


jQuery(document).ready( function() {
	
	// ouvrir ma sélection
	jQuery("#maselection").click(function(e){
			e.preventDefault();
			// on ferme newsletter s'il est ouvert :)
			if(jQuery('#module-newsletter').is(':visible')) {
				jQuery('#module-newsletter').hide('blind',500);
			}
			if(jQuery('#module-recommander').is(':visible')) {
				jQuery('#module-recommander').hide('blind',500);
			}					
			jQuery('#container-selection').toggle('blind','slow');
	});	
	
	// suppression d'un item
	jQuery('.itemdel').live('click',function() {
		jQuery.post(jQuery(this).attr('href'),'toto=1',function(data){
			if(data.response == true)
			{
				jQuery("#itemselection1").fadeOut(1000,function(){
					var count = jQuery('#itemcount').text()-1;
					jQuery('#itemcount').text(count);
				}).remove();
			}
		},'json');
		return false;
	});

	// suppression d'un item
	jQuery('#itemdelform').live('click',function() {
		jQuery.post(jQuery(this).attr('href'),'toto=1',function(data){
			if(data.response == true)
			{
				jQuery("#container-selection-form").remove();
				jQuery("#datetime").html(data.datetime);
			}
		},'json');
		return false;
	});
	
	jQuery('.itemadd').live('click',function() {
		// on ferme newsletter s'il est ouvert :)
		if(jQuery('#module-newsletter').is(':visible')) {
			jQuery('#module-newsletter').hide('blind',500);
		}
		if(jQuery('#module-recommander').is(':visible')) {
			jQuery('#module-recommander').hide('blind',500);
		}
		var count = (parseInt(jQuery('#itemcount').text())+1);
			jQuery.post(jQuery(this).attr('href'),'toto=1',function(data){
				
				jQuery(".itemend").show();				
				html ='<div id="itemselection1" class="itemselection"><input type="hidden" id="xx'+data.id+'" name="xx'+data.id+'" value="'+data.id+'" /><div class="imgcrop fleft mright10" style="background:url('+data.options.image+') center center no-repeat;"><a href="'+data.options.url+'" class="dblock wh100p"></a></div><div class="fleft h62 w308"><h5>'+data.options.titre+'</h5><h6>'+data.options.stitre+'</h6><a href="'+data.options.url+'" class="bleuc"><span>' + langMySelect.voir + '</span></a><div class="clearer"></div><a href="'+data.site_web+'/selection/delitemselection/'+data.rowid+'" class="itemdel">' + langMySelect.supprimer + '</a></div><div class="clearer"></div></div>';
				if(jQuery("#xx"+data.id).val()){ 
				} else {
					jQuery("#container-selection").prepend(html);
					jQuery('#itemcount').text(count);
					if(jQuery('.itemadd').data('openpanel') == true){
						if(jQuery('#container-selection').is(':hidden')){
							jQuery('#container-selection').show('blind',500);
						}
					} else {
						jQuery('.itemadd').data('openpanel',true);
					}
				}
				
			},'json');			
		return false;
	});
	
});
