// MOSNE FUNCTIONS
$(function(){
      
   
   // feature test for position fixed.

Modernizr.addTest('positionfixed', function () {
    var test    = document.createElement('div'),
        control = test.cloneNode(false),
        fake = false,
        root = document.body || (function () {
            fake = true;
            return document.documentElement.appendChild(document.createElement('body'));
        }());

    var oldCssText = root.style.cssText;
    root.style.cssText = 'padding:0;margin:0';
    test.style.cssText = 'position:fixed;top:42px'; 
    root.appendChild(test);
    root.appendChild(control);
    
    var ret = test.offsetTop !== control.offsetTop;

    root.removeChild(test);
    root.removeChild(control);
    root.style.cssText = oldCssText;
    
    if (fake) {
        document.documentElement.removeChild(root);
    }

    return ret;
});



var $wall = $("#pool");
        
   if ($("body").hasClass("home")){
        
    //    wh = $(window).height();
    //    $('#megapattern').css({"height":wh});
    
        $("#megapattern").click(function(){
            $("#megapattern").fadeOut(1200);
        });

    /*
        var status = $.cookie("status");
        
        if (status){}else{
        $("#megapattern").show();
        $("#megapattern").click(function(){
            $("#megapattern").fadeOut(1200);
        });
        var date = new Date();
        date.setTime(date.getTime() + (3600 * 1000));
        $.cookie("status",true,{ expires: date});
        }
      */  
    
    }
    
    
    // position fixed on touch devices
    
    if (!Modernizr.touch && !Modernizr.positionfixed) {
     
     var fixed_map = function(){
     	$("#sidebar,.sausage-set:eq(0)").css({'-webkit-transform':'translate3d(0,'+window.pageYOffset+'px, 0)'});
     };
     
     
       $("#sidebar,.sausage-set:eq(0)").css({'-webkit-transition': '-webkit-transform 1s ease-in-out','-webkit-transform': 'translateY('+window.pageYOffset+'px)'});
       $(document).bind('scroll touchmove resize orientationchange',function(){fixed_map()});
     
     
    
    $("#bd").bind("sscroll",function(){
        fixed_map();
    });
    
    }
    
         
   //sausage -desktop only
    
       if (!Modernizr.touch &&  !$("body").hasClass("page-id-7")){
        $(window).sausage({
            content: function (i, $page) {
            tt= $("h1 a",$page).first().text();
            s_title = tt.replace(/^([^\s]*)(.*)/, '$1');
            return '<span class="sausage-span">' + tt + '</span>';
        }});
   
        var ck = true;
   }     
    
    
   
	//video
	$('a.embedvideo').live("click",function(e){
    
    e.preventDefault();
       
       $(this).hide();
       url = $(this).attr("href");
    
  //   v_id = url.match("[\?&]v=([^&#]*)")[1];
  //   i_src = "http://www.youtube.com/embed/"+v_id+"?controls=0&showinfo=0&autoplay=1&hd=1&fs=1&rel=0";
       
        var regExp = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
                var match = url.match(regExp);
                if (match){
                    //vimeo
                    v_id = match[2];
                    i_src = "http://player.vimeo.com/video/"+v_id+"?title=0&byline=0&portrait=0&color=ffffff&autoplay=1";   
                }else{
                    //youtube
                    v_id = url.match("[\?&]v=([^&#]*)")[1];
                    i_src = "http://www.youtube.com/embed/"+v_id+"?controls=0&showinfo=0&autoplay=1&hd=1&fs=1&rel=0";
                }  


       whr = $(this).parent();
       ih = whr.find("img").height();
       iw = whr.find("img").width();
       whr.empty().append('<iframe width="'+iw+'" height="'+ih+'" src="'+i_src+'"></iframe>');
    
    
  }); 
  
    //flash
      
if ($.flash.available){


	$('a.flash').live("click",function(e){
    
    e.preventDefault();
       
       $(this).hide();
       url = $(this).attr("href");
       whr = $(this).parent();
       ih = whr.find("img").height();
       iw = whr.find("img").width();
   
       var m_flash = $.flash.create({
		swf: url,
		height: ih,
		width: iw,
		allowFullScreen: true,
		wmode: 'transparent',
	    menu: false
     });
     
     $(whr).html(m_flash);
    
  }); 
    
}else{
    $('a.flash').hide();
}


 $("#bd").bind("ssup",function(){
        $("#pool").imagesLoaded(function(){
               $(window).sausage('draw');
               //log("up");
    });
    
    //log("noup");
   }).trigger("ssup");
   
   
 
    //infinite scroll
	
	$("#pool").infinitescroll({
        navSelector  : '#page_nav',   
        nextSelector : '#page_nav a:first', 
        itemSelector : '.page',      
        loading: {
                finishedMsg: "FIN",
                img: "/wp-content/themes/we-we/images/trasp.gif",
                msgText: "LOADING...",
        },
        debug: false,
        },
        function( newElements ) {
        if (ck){
            var $newElems = $( newElements );
            //log("dwn");     
            setTimeout(function(){$(window).sausage('draw');},600);
            $newElems.imagesLoaded(function(){
                $(window).sausage('draw');
            });
        }
      });

  
    
    
    //map
  if($("body").hasClass("page-id-7") ){
   
  if($("html").hasClass("no-touch")){
   
    wh = $(window).height();
    $('#map').css({"height":wh});
        
   }
   
    var myhue= "#D1CCC3";
    var stylez=[{featureType:"water",elementType:"all",stylers:[{invert_lightness:true},{lightness:26},{saturation:-90},{hue:myhue}]},{featureType:"road",elementType:"all",stylers:[{hue:myhue},{visibility:"on"},{saturation:-81},{lightness:12}]},{featureType:"administrative",elementType:"all",stylers:[{lightness:28},{hue:myhue},{saturation:-73}]},{featureType:"landscape",elementType:"all",stylers:[{visibility:"on"},{saturation:-87}]},{featureType:"poi",elementType:"all",stylers:[{saturation:-77},{hue:myhue}]},{featureType:"road.highway",elementType:"all",stylers:[{visibility:"simplified"}]},{featureType:"road.local",elementType:"all",stylers:[{hue:myhue},{saturation:10},{lightness:50}]},{featureType:"transit",elementType:"labels",stylers:[{visibility:"on"},{hue:myhue},{saturation:-87},{lightness:32}]}];
    
   var mybg_color="#D1CCC3";
   var cluster_styles=[{url:'/wp-content/themes/we-we/images/m4_lt.png',height:40,width:40,opt_textSize:17,opt_anchor:[6,0],opt_textColor:'#fff'},{url:'/wp-content/themes/we-we/images/m4_lt.png',height:40,width:40,opt_textSize:17,opt_anchor:[6,0],opt_textColor:'#fff'},{url:'/wp-content/themes/we-we/images/m4_lt.png',height:40,width:40,opt_textSize:17,opt_anchor:[6,0],opt_textColor:'#fff'},{url:'/wp-content/themes/we-we/images/m4_lt.png',height:40,width:40,opt_textSize:17,opt_anchor:[6,0],opt_textColor:'#fff'}];
   var marker_icon='/wp-content/themes/we-we/images/m_ilmf.png';
   var marker_icon_poi='/wp-content/themes/we-we/images/m_ilmf.png';
   var mapstyle_id='minimal';var mapOptions={mapTypeControlOptions:{mapTypeIds:[google.maps.MapTypeId.ROADMAP,mapstyle_id]},panControl:false,zoomControl:true,zoomControlOptions:{style:google.maps.ZoomControlStyle.SMALL,position:google.maps.ControlPosition.TOP_LEFT},mapTypeControl:false,scaleControl:false,streetViewControl:false,zoom:6,disableDoubleClickZoom:true,backgroundColor:mybg_color};
   
   var s_infobox_g = {
			 content: "",
			 disableAutoPan: false,
			 maxWidth: 0,
			 pixelOffset: new google.maps.Size(-110, 5),
			 zIndex: null,
			 boxStyle: { 
			 background: "url('/wp-content/themes/we-we/images/infobox_top.png') center top no-repeat",
			 color:'#333',
			 padding: '0',
			 width: "220px"
			 },
			  closeBoxMargin: "16px 4px",
			 closeBoxURL: "/wp-content/themes/we-we/images/infobox_close.png",
		     infoBoxClearance: new google.maps.Size(1, 1),
			 isHidden: false,
			 pane: "floatPane",
			 enableEventPropagation: false
		};
		
  $("#map").mosne_map({
    elements: '.location',
    infowindows: false,
    infobox: true,
    infobox_s:s_infobox_g,
    timeout: 100,
    clat: 48.849841664625316,
    clng: 2.3798274993896484,
    clickedzoom: 17,
    mapstyle: stylez,
    mapstyle_name: mapstyle_id,
    map_opt: mapOptions,
    cluster_styles: {
        zoomOnClick: true,
        maxZoom: 13,
        gridSize: 30,
        styles: cluster_styles
    },
    marker_icon: marker_icon,
    after: function () {
        setTimeout(function(){
            $(".maplink").trigger("click");
        },800);
    }

}); 


    
   
  
  }
    	
	
}); //scope 






