// JavaScript Document















function stripeMe() {

	$(".stripe-me tbody tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});

	$(".stripe-me tbody tr:even").addClass("alt");

}





/* Original from: http://github.com/kvz/phpjs/raw/master/functions/strings/strpos.js  : BEGIN */

function strpos (haystack, needle, offset) {

    // http://kevin.vanzonneveld.net

    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

    // +   improved by: Onno Marsman    

    // +   bugfixed by: Daniel Esteban

    // +   improved by: Brett Zamir (http://brett-zamir.me)

    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);

    // *     returns 1: 14



    var i = (haystack+'').indexOf(needle, (offset || 0));

    return i === -1 ? false : i;

}

/* Original from: http://github.com/kvz/phpjs/raw/master/functions/strings/strpos.js  : END */











function destroyMessageBox() {

	setTimeout('$(".message-box").slideUp("slow", function(){ $(this).detach()});', 3000);

}













function highLightField(selector) {

	$(selector).focus(function() {

		$(this).addClass("form-field-active");

	}).blur(function() {

		$(this).removeClass("form-field-active");

	});

}









function image_loader(container, imageSrc, style) {

	var img = new Image();

	var maxWidth = $(container).width();

	var maxHeight = $(container).height();

	//alert ('document dimensions: ' + maxWidth + ' x '+ maxHeight);

	//$(container).css("height", maxHeight + "px").attr("height", maxHeight);

	$(container).addClass("loading");

	$(img).load(function () {

		$(this).hide();

		var imgWidth = $(this).attr("width");

		var imgHeight = $(this).attr("height");

		//alert ("Imagem dimensionss: " + imgWidth + ' x '+ imgHeight);

		

/*

		var displayWidth = $(container).width();

		var displayHeight = $(container).height();

		//alert ("display dimensions: " + displayWidth + ' x '+ displayHeight);



		if (displayHeight > maxHeight) {

			displayHeight = maxHeight;

		}

		if (displayWidth > maxWidth) {

			displayWidth = maxWidth;

		}

		var Xratio = imgWidth / displayWidth;

		var Yratio = imgHeight / displayHeight;

		

		var newWidth = 0;

		var newHeight = 0;

		if (Xratio < Yratio) {

			newWidth = (imgWidth / Xratio) | 0;

			newHeight = (imgHeight / Xratio) | 0;

		} else {

			newWidth = (imgWidth / Yratio) | 0;

			newHeight = (imgHeight / Yratio) | 0;

		}

	*/

		var newHeight = 0;

		var newWidth = 0;



		if (style == 0) {

			newWidth = imgWidth;

			newHeight = imgHeight;

			//alert("new dimensions: " + newWidth + " x " + newHeight);

			//alert("new dimensions: " + halfWidth + " x " + halfHeight);

		}

		if (style == 1) {

			//alert(style);

			var displayWidth = $(container).width();

			var displayHeight = $(container).height();

			if (displayHeight > maxHeight) {

				displayHeight = maxHeight;

			}

			if (displayWidth > maxWidth) {

				displayWidth = maxWidth;

			}

			//alert("Display: "  +displayWidth+ " x " + displayHeight);

			

			if ((imgHeight > displayHeight) || (imgWidth > displayWidth)) {

				Xratio = 1;

				Yratio =1;

				if (imgHeight > displayHeight) {

					Yratio = imgHeight / displayHeight;

				}

				if (imgWidth > displayWidth) {

					Xratio = imgWidth / displayWidth;

				}

				if (Xratio > Yratio) {

					newWidth = displayWidth;

					newHeight = (imgHeight / Xratio) | 0;

				} else {

					newHeight = displayHeight;

					newWidth = (imgWidth / Yratio) | 0;

				}

				//alert('New size: '+ newWidth + '  ' + newHeight);

				

			} else {

				newWidth = imgWidth;

				newHeight = imgHeight;

			}			

			//alert("new dimensions: " + newWidth + " x " + newHeight);

		}

		$(container + " img").each(function() {

			$(this).fadeOut().remove();

		});



		halfHeight = (newHeight / 2) | 0;

		halfWidth = (newWidth / 2) | 0;

		

		//alert(' Half: ' + halfWidth + '  ' + halfHeight);



		$(container).removeClass('loading').append(this);

		if (style == 0) {

			$(this).css({"margin-left": "-"+halfWidth+"px", "margin-top": "-"+halfHeight+"px"});

		}

		if (style == 1) {

			//alert("halves: " + halfWidth + " x " + halfHeight);

			$(this).attr({"width": newWidth, "height": newHeight}).css({"margin-left":"-"+halfWidth+"px"}).css({"margin-top":"-"+(halfHeight+1)+"px"});

		}





		$(this).fadeIn();

	}).error(function () {

		// notify the user that the image could not be loaded

	}).attr('src', imageSrc);

}















function setup_ajax_links() {
	$(".pagination > a").each(function(index) {
		var href = $(this).attr("href");
		if ( $(this).hasClass('ajax-bbq') == false) {
			$(this).addClass("ajax-bbq");
		}
	});


	
	$(".ajax-bbq").unbind('click').bind('click', function() {
		var state = {},
		
		// Get the url from the link's href attribute, stripping any leading #.
		url = $(this).attr( 'href' );

		//Is this a link that should always push new content?
		var dynamic = $(this).hasClass("dynamic");
		if (dynamic) {
			url += '?' + Math.random();
		}
		// Set the state!
		state[ 'content' ] = url;
		$.bbq.pushState( state );

		return false;
	});

	//Player link
	$('.playerImagePlay').unbind('click').click(
		function () {
			if ($(this).hasClass('playerImagePlayOn')) {
				This = this;
				playerPlayClick(This);
			}
		}
	);
	$('.playerContainer').append("<img src='/images/dot.gif' class='playerPosition playerPositionOff' />");
	$('.playerContainer').append("<img src='/images/dot.gif' class='playerImagePause playerImagePauseOff' />");
	$('.playerContainer').append("<img src='/images/dot.gif' class='playerImageStop playerImageStopOff' />");
	
	

	//Kill left over tooltips
	$(".tooltip").remove();

}


/*BBQ Stuff*/

var enable_cache = false;

$(function(){

	//var url = document.location.replace( /^#/, '' );
	//alert('url '+url);

	




	  // For each .bbq widget, keep a data object containing a mapping of

	  // url-to-container for caching purposes.

	  $('.bbq').each(function(){

	    $(this).data( 'bbq', {

	      cache: {

	        // If url is '' (no fragment), display this div's content.

	        '': $(this).find('.bbq-default')

	      }

	    });

	  });

	  

	  // For all links inside a .bbq widget, push the appropriate state onto the

	  // history when clicked.

	  $('.bbq a[href^=#]').live( 'click', function(e){

	    var state = {},

	      

	      // Get the id of this .bbq widget.

	      id = $(this).closest( '.bbq' ).attr( 'id' ),

	      

	      // Get the url from the link's href attribute, stripping any leading #.

	      url = $(this).attr( 'href' ).replace( /^#/, '' );

	    

	    //Is it a "dynamic" marked content?

	    var dynamic = $(this).hasClass("dynamic");

	    if (dynamic) {

	    	url += '?' + Math.random();

	    }

	    // Set the state!

	    state[ id ] = url;

	    $.bbq.pushState( state );

	    

	    // And finally, prevent the default link click behavior by returning false.

	    return false;

	  });

	  

	
	setup_ajax_links();


	// Bind an event to window.onhashchange that, when the history state changes,

	// iterates over all .bbq widgets, getting their appropriate url from the

	// current state. If that .bbq widget's url has changed, display either our

	// cached content or fetch new content to be displayed.

	$(window).bind( 'hashchange', function(e) {


		var hash_str = e.fragment;
		param_obj = e.getState();
		param_val = e.getState('content');
		//alert( param_val );

		if (param_val != '') {
			//alert('loading '+param_val);
			$.ajax({
				url: param_val,
				dataType: 'json',
				data: { },
				success: function(response) {
					$("#content").html(response.html);
					//alert(response.is_frontpage);
					if (response.is_frontpage == false) {
						if (response.slug) {
							if (response.slug == 'spectacle') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/spectacle_top.jpg">'); }
							if (response.slug == 'nouvelle_coproduction_jmf_orchestre_de_paris') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/spectacle_top.jpg">'); }
							if (response.slug == 'reseau') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/region_top.jpg">'); }
							if (response.slug == 'news') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/new_top.jpg">'); }
							if (response.slug == 'partenaires') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/partenaires_top.jpg">'); }
							if (response.slug == 'contacts') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/contact_top.jpg">'); }
							if (response.slug == 'documentation') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/spectacle_top.jpg">'); }
							if (response.slug == 'associative') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/spectacle_top.jpg">'); }
							if (response.slug == 'rapports') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/spectacle_top.jpg">'); }
							if (response.slug == 'votre') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/region_top.jpg">'); }
							if (response.slug == 'reponses') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/reponses_top.jpg">'); }
							if (response.slug == 'pagedaccueil') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/spectacle_top.jpg">'); }
							 if (response.slug == 'concerts') { $("#image_top").html('<img width="899" height="87" border="0" src="/images/region_top.jpg">'); 	}
						}
						$("#head_home").replaceWith('<div id="head">'+
							'<div id="headmenu">'+
							'<ul>'+
							'<li id="menu1"><a class="ajax-bbq" id="home_menu" href="/read/pagedaccueil"><img width="58" height="17" border="0" src="images/home_menu.jpg"> </a></li>'+
							'<li id="menu2"><a class="ajax-bbq" id="spectacles_menu" href="/spectacles/parcourir"><img width="75" height="17" border="0" src="images/spectacles_menu.jpg"></a></li>'+
							'<li id="menu3"><a class="ajax-bbq" id="reseau_menu" href="/reseau/cartefrance"><img width="55" height="17" border="0" src="images/reseau_menu.jpg"> </a></li> '+
							'<li id="menu4"><a class="ajax-bbq" id="news_menu" href="/news/parcourir"><img width="72" height="17" border="0" src="images/news_menu.jpg"></a></li>'+
							'<li id="menu5"><a class="ajax-bbq" id="libraire_menu" onmouseout="delayhidemenu()" onmouseover="dropdownmenu(this, event, quebec, \'140px\')" onclick="return clickreturnvalue()" href="#"><img width="63" height="17" border="0" src="images/libraire_menu.jpg"></a></li>'+
							'<li id="menu6"><a class="ajax-bbq" id="partenaire_menu" href="/read/partenaires"><img width="84" height="17" border="0" src="images/partenaire_menu.jpg"></a></li>'+
							'<li id="menu7"><a class="ajax-bbq" id="contact_menu" href="/read/contacts"><img width="65" height="17" border="0" src="images/contact_menu.jpg"></a></li>'+
							'</ul>'+
							'</div>'+
							'</div>');
						$("#home_image").css({display: "none"});
					} else {
						$("#image_top").html('');
						$("#head").replaceWith('<div id="head_home">'+
							'<div id="logo"><a href="/read/pagedaccueil" class="ajax-bbq"><img width="143" height="57" border="0" src="/images/logo.jpg"></a></div>'+
							'<div id="headmenu_home">'+
							'<ul>'+
							'<li id="menu1"><a id="home_menu" href="/read/pagedaccueil" class="ajax-bbq"><img width="58" height="17" border="0" src="/images/home_menu.jpg"> </a></li>'+
							'<li id="menu2"><a id="spectacles_menu" href="/spectacles/parcourir" class="ajax-bbq"><img width="75" height="17" border="0" src="/images/spectacles_menu.jpg"></a></li>'+
							'<li id="menu3"><a id="reseau_menu" href="/reseau/cartefrance" class="ajax-bbq"><img width="55" height="17" border="0" src="/images/reseau_menu.jpg"> </a></li>'+
							'<li id="menu4"><a id="news_menu" href="/news/parcourir" class="ajax-bbq"><img width="72" height="17" border="0" src="/images/news_menu.jpg"></a></li>'+
							'<li id="menu5"><a id="libraire_menu" onmouseout="delayhidemenu()" onmouseover="dropdownmenu(this, event, quebec, \'140px\')" onclick="return clickreturnvalue()" href="#" class="ajax-bbq"><img width="63" height="17" border="0" src="/images/libraire_menu.jpg"></a></li>'+
							'<li id="menu6"><a id="partenaire_menu" href="/read/partenaires" class="ajax-bbq"><img width="84" height="17" border="0" src="/images/partenaire_menu.jpg"></a></li>'+
							'<li id="menu7"><a id="contact_menu" href="/read/contacts" class="ajax-bbq"><img width="65" height="17" border="0" src="/images/contact_menu.jpg"></a></li>'+
							'</ul>'+
							'</div>'+
							'</div>');
						$("#home_image").css({display: "block"});
					}
					setup_ajax_links();
				}
			});
		}

	    // Iterate over all .bbq widgets.

	    $('.bbq').each(function(){

	      var that = $(this),

	        

	        // Get the stored data for this .bbq widget.

	        data = that.data( 'bbq' ),

	        

	        // Get the url for this .bbq widget from the hash, based on the

	        // appropriate id property. In jQuery 1.4, you should use e.getState()

	        // instead of $.bbq.getState().

	        url = $.bbq.getState( that.attr( 'id' ) ) || '';

	      

	      // If the url hasn't changed, do nothing and skip to the next .bbq widget.

	      if ( data.url === url ) { return; }

	      

	      // Store the url for the next time around.

	      data.url = url;

	      

	      // Remove .bbq-current class from any previously "current" link(s).

	      that.find( 'a.bbq-current' ).removeClass( 'bbq-current' );

	      

	      // Hide any visible ajax content.

	      that.find( '.bbq-content' ).children( ':visible' ).hide();

	      

	      // Add .bbq-current class to "current" nav link(s), only if url isn't empty.

	      url && that.find( 'a[href="#' + url + '"]' ).addClass( 'bbq-current' );

	      

	      if (( data.cache[ url ] ) && enable_cache) {

	        // Since the widget is already in the cache, it doesn't need to be

	        // created, so instead of creating it again, let's just show it!

	        data.cache[ url ].show();

	        

	      } else {

	        // Show "loading" content while AJAX content loads.

	        that.find( '.bbq-loading' ).show();

	        

	        // Create container for this url's content and store a reference to it in

	        // the cache.

	        data.cache[ url ] = $( '<div class="bbq-item"/>' )

	          

	          // Append the content container to the parent container.

	          .appendTo( that.find( '.bbq-content' ) )

	          

	          // Load external content via AJAX. Note that in order to keep this

	          // example streamlined, only the content in .infobox is shown. You'll

	          // want to change this based on your needs.

	          

	         // .load( url, function(){

	          //  // Content loaded, hide "loading" content.

	         //   that.find( '.bbq-loading' ).hide();

	          //});

	          

	          	//TO-DO Process response as an array of DOM elements to be replaced

	          if (url != '') {

				  alert('loading '+url);
				$.ajax({

					url: url,

					dataType: 'json',

					data: { teste: "123"},

					success: function(response) {

						$("#ajax_content").html(response.middle);

					}
				});

	          }

	        

	      }

	    });

	  })

	  

	  //TO-DO: Check if there's no hash, so the page would load with no extra ajax content

	  

	  // Since the event is only triggered when the hash changes, we need to trigger

	  // the event now, to handle the hash the page may have loaded with.

	  $(window).trigger( 'hashchange' );

	  

	});












