$(document).ready(function(){
	
	$(function () {
			
		if($.browser.msie){
			var img = $("a:not(#logo) img");
		}else{
			var img = $("a img");
		}
		$('.cta').find('a').each(function () {
			var $img = $('img.hover', this).css("opacity", "0");
			var $img = $('img.hover', this).css("display", "block");
			$(this).hover(function () {
				$img.stop().fadeTo(150, 1);
			}, function () {
				$img.stop().fadeTo(450, 0);
			});
		});
//////////Shrink my titles
		$('.h1Titles').excerpt();
		
//////////open links in external window
		$('a[rel="external"]').click( function() {
			window.open( $(this).attr('href') );
			return false;
   		 });

////////// 3 forfaits
		$(".produit1").hover(function(){
			$(this).toggleClass('produit1Hover');
		});
		$(".produit2").hover(function(){
			$(this).toggleClass('produit2Hover');
		});
		$(".produit3").hover(function(){
			$(this).toggleClass('produit3Hover');
		});

/////////Smooth top menu
		$('li.page-item-35 a')
		.css( {backgroundPosition: "0 10px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(0 53px)"}, {duration:300})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(0 10px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "0 10px"})
			}})
		})
		$('li.page-item-3 a')
		.css( {backgroundPosition: "-99px 10px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-99px 53px)"}, {duration:300})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-99px 10px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-99px 10px"})
			}})
		})
		$('li.page-item-30 a')
		.css( {backgroundPosition: "-296 10px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-296 53px)"}, {duration:300})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-296 10px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-296 10px"})
			}})
		})
		$('li.page-item-37 a')
		.css( {backgroundPosition: "-429 10px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-429 53px)"}, {duration:300})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-429 10px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-429 10px"})
			}})
		})
		$('li.page-item-32 a')
		.css( {backgroundPosition: "-589 10px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(-589 53px)"}, {duration:300})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(-589 10px)"}, {duration:200, complete:function(){
				$(this).css({backgroundPosition: "-589 10px"})
			}})
		})
		
//////////// scroll to top
	var scrolltotop={
	//startline: Integer. Number of pixels from top of doc scrollbar is scrolled before showing control
	//scrollto: Keyword (Integer, or "Scroll_to_Element_ID"). How far to scroll document up when control is clicked on (0=top).
	setting: {startline:100, scrollto: 0, scrollduration:1000, fadeduration:[500, 100]},
	controlHTML: '<img src="/wp-content/themes/AussiSimple/img/back-to-top.png" style="width:28px; height:43px" />', //HTML for control, which is auto wrapped in DIV w/ ID="topcontrol"
	controlattrs: {offsetx:5, offsety:30}, //offset of control relative to right/ bottom of window corner
	anchorkeyword: '#top', //Enter href value of HTML anchors on the page that should also act as "Scroll Up" links

	state: {isvisible:false, shouldvisible:false},

	scrollup:function(){
		if (!this.cssfixedsupport) //if control is positioned using JavaScript
			this.$control.css({opacity:0}) //hide control immediately after clicking it
		var dest=isNaN(this.setting.scrollto)? this.setting.scrollto : parseInt(this.setting.scrollto)
		if (typeof dest=="string" && jQuery('#'+dest).length==1) //check element set by string exists
			dest=jQuery('#'+dest).offset().top
		else
			dest=0
		this.$body.animate({scrollTop: dest}, this.setting.scrollduration);
	},

	keepfixed:function(){
		var $window=jQuery(window)
		var controlx=$window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx
		var controly=$window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety
		this.$control.css({left:controlx+'px', top:controly+'px'})
	},

	togglecontrol:function(){
		var scrolltop=jQuery(window).scrollTop()
		if (!this.cssfixedsupport)
			this.keepfixed()
		this.state.shouldvisible=(scrolltop>=this.setting.startline)? true : false
		if (this.state.shouldvisible && !this.state.isvisible){
			this.$control.stop().animate({opacity:1}, this.setting.fadeduration[0])
			this.state.isvisible=true
		}
		else if (this.state.shouldvisible==false && this.state.isvisible){
			this.$control.stop().animate({opacity:0}, this.setting.fadeduration[1])
			this.state.isvisible=false
		}
	},
	
	init:function(){
		jQuery(document).ready(function($){
			var mainobj=scrolltotop
			var iebrws=document.all
			mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode
			mainobj.$body=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body')
			mainobj.$control=$('<div id="topcontrol">'+mainobj.controlHTML+'</div>')
				.css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', bottom:mainobj.controlattrs.offsety, right:mainobj.controlattrs.offsetx, opacity:0, cursor:'pointer'})
				.attr({title:'Remonter en haut de la page'})
				.click(function(){mainobj.scrollup(); return false})
				.appendTo('body')
			if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') //loose check for IE6 and below, plus whether control contains any text
				mainobj.$control.css({width:mainobj.$control.width()}) //IE6- seems to require an explicit width on a DIV containing text
			mainobj.togglecontrol()
			$('a[href="' + mainobj.anchorkeyword +'"]').click(function(){
				mainobj.scrollup()
				return false
			})
			$(window).bind('scroll resize', function(e){
				mainobj.togglecontrol()
			})
		})
	}
}

scrolltotop.init()
		
//////////When you look at me ... I completely disappear
		$("#top").find("input").each(function(){
			$(this).focus(function () {
				$(this).val("");
			});
		});

//////////Cookie monster just around the corner
		function setCookie(name,value,expiredays){
			var exdate=new Date();
			exdate.setDate(exdate.getDate()+expiredays);
			document.cookie=name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/;domains=";
		}
		
		function getCookie(c_name){
			if (document.cookie.length>0){
			  c_start=document.cookie.indexOf(c_name + "=");
				  if (c_start!=-1){ 
				    c_start=c_start + c_name.length+1; 
				    c_end=document.cookie.indexOf(";",c_start);
				    if (c_end==-1) c_end=document.cookie.length;
				    return unescape(document.cookie.substring(c_start,c_end));
			    } 
			}
		return "";
		}
		

	
		
//////////slide me up and down
		$(".control").hide();
		$("#containerFlash").hover(function(){
				$(".control").show();
			},function(){
				$(".control").hide();
			});
		
		var getSliderCookie = getCookie("sliderSize");
		if(getSliderCookie!=null && getSliderCookie!=""){
			var getSliderCookie = parseInt(getSliderCookie, 10);
			$('#containerFlash').height(getSliderCookie);
		};
		
		var sizeOri = 350;
		var sizeNew = 172;
		var classOri = 'up';
		var classNew = 'down';
		
		if (getSliderCookie == 172) {
			var sizeOri = 172;
			var sizeNew = 350;
			var classOri = 'down';
			var classNew = 'up';
		};
		
		$(".toggle").addClass(classOri);
		$(".toggle").toggle(function(){
			$("#containerFlash").animate({
       			height:sizeNew
			},
    		500,
    		"swing",
    		function(){
    			$(".toggle").removeClass(classOri);
    			$(".toggle").addClass(classNew);
    			setCookie('sliderSize',sizeNew,365);
			});
			
		},function(){
			$("#containerFlash").animate({
       			height:sizeOri
			},
    		500,
    		"swing",
    		function(){
    			$(".toggle").removeClass(classNew);
    			$(".toggle").addClass(classOri);
    			setCookie('sliderSize',sizeOri,365);
			});
			
		});
		
		$('#bannerFlash').cycle({ 
		    fx:    'fade', 
		    speed:  1500,
		    next:   '.next', 
    		prev:	'.prev'
		 });
		 
		 
//////////Make me bigger or smaller, I don't care		
		
		// Original Font Size
		var originalFontSize = $('#post').css('font-size');
		if ($.browser.msie) {
			var originalFontSize = parseInt(originalFontSize, 10);
		}
		$("#normal").click(function(){
			$('#post').css('font-size', originalFontSize);
			setCookie('textSize',""+ originalFontSize + "",365);
			return false;
		});
		
		var getSizeCookie = getCookie("textSize");
		if(getSizeCookie!=null && getSizeCookie!=""){
			var getSizeCookieNum = parseInt(getSizeCookie, 10);
			$('#post').css('font-size', getSizeCookieNum);
		};
		
		// Increase Font Size
		$("#big").click(function(){
			var currentFontSize = $('#post').css('font-size');
			var currentFontSizeNum = parseInt(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*1.2;
			$('#post').css('font-size', newFontSize);
			setCookie('textSize',""+ newFontSize + "",365);
			return false;
		});
		// Decrease Font Size
		$("#small").click(function(){
			var currentFontSize = $('#post').css('font-size');
			var currentFontSizeNum = parseInt(currentFontSize, 10);
			var newFontSize = currentFontSizeNum*0.8;
			$('#post').css('font-size', newFontSize);
			setCookie('textSize',""+ newFontSize + "",365);
			return false;
		});
		
		
//////////Print me me me...
		$("#print").click(function(){
			window.print();
		});
		

/////////She's The Form, you have to do what she say...
		/*$(":text").each(function(){
			$(this).addClass("text");
		});
		$(":password").each(function(){
			$(this).addClass("password");
		});
		$(":checkbox").each(function(){
			$(this).addClass("checkbox");
		});
		$(":submit").each(function(){
			$(this).addClass("submit");
		});
		$(":button").each(function(){
			$(this).addClass("submit");
		});
		*/	
			
//////////Put some decoration on my links
		$("a[href$=pdf]").addClass("pdf").attr("target","_blank");
		$("a[href$=xls]").addClass("xls").attr("target","_blank");
		
//////////Tell me where i'm going
		$("a[rel='3m4il']").each(function(){
			var spaceShip = $(this).text();
			var spaceStation = $(this).attr('href');
			
			$(this).attr({
				href: 'mailto:'+ spaceShip +'@'+ spaceStation +'',
				rel: 'nofollow'
			});
			$(this).text(''+ spaceShip +'@'+ spaceStation +'');
		});
		
//////////Please help me J.J. Abrams i'm lost...
		if($("#siteMap").length !=0) {
			$("#siteMap").treeview({
				collapsed: false,
				animated: "medium",
				control:"#expandCollapse",
				persist: "location"
			});
		};

//////////Gmap
		if ($('#gmap').length != 0) {
				function initialize() {
					var latlng = new google.maps.LatLng(47.439463, -69.978756);
					var myOptions = {
						zoom: 14,
						center: latlng,
						noClear:false,
						mapTypeControl: true,
						mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
						navigationControl: true,
						navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
						mapTypeId: google.maps.MapTypeId.ROADMAP
					};
					var map = new google.maps.Map(document.getElementById("gmap"), myOptions);
					
					var marker = new google.maps.Marker({
						position: latlng, 
						map: map
						
					
					});
				};
				initialize();
		};

			
	});	
	/**
 * @author Alexander Farkas
 * v. 1.02
 */
(function($) {
	$.extend($.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);
});
