(function($){
	$(document).ready(function(){
		$(window).load(function(){
			$('#header .fade').fadeIn(1500);
		})
		
	
		/* ====== Centering the content of the flip boxes ===== */
		$('.questionsListHolder .questionFlip').each(function(){
			var height = $(this).find('.questcont').innerHeight();
			height = 72 - height;
			$(this).find('.questcont').css('padding-top', height/2 + 'px' );
		})
		$('.sub-menu').hide();
		$('#navigation ul li').hover(function(){
		
			var theme_path = $(document).find('#audio-player').attr('title');
			var audio = '/audio-player/hover2.mp3';
			audio = theme_path + audio;
			play_audio(audio);
			$(this).find('.sub-menu:eq(0)').show();
			$(this).find('a:eq(0)').addClass('hover');
		},
		function(){
			$(this).find('.sub-menu').hide();
			$(this).find('a:eq(0)').removeClass('hover');
		});
		
		$('#navigation ul li ul li').each(function(){
			var index = $(this).index();
			var top = index * 24;
			if ( ! $.browser.msie ) {
				$(this).find('.sub-menu').css('top', top + 'px' );
			};
		})
		
		$('.questionFlip').bind("click",function(){
		
			// $(this) point to the clicked .sponsorFlip element (caching it in elem for speed):
			
			var elem = $(this);
			
			var theme_path = $(document).find('#audio-player').attr('title');
			var audio = '/audio-player/click2.mp3';
			audio = theme_path + audio;
			play_audio(audio);
			
			// data('flipped') is a flag we set when we flip the element:
			
			if(elem.data('flipped')) {
				// If the element has already been flipped, use the revertFlip method
				// defined by the plug-in to revert to the default state automatically:
				
				elem.revertFlip();
				
				// Unsetting the flag:
				elem.data('flipped',false)
			} else {
				// Using the flip method defined by the plugin:
				elem.flip({
					direction:'lr',
					speed: 200,
					onBefore: function(){
						// Insert the contents of the .sponsorData div (hidden from view with display:none)
						// into the clicked .sponsorFlip div before the flipping animation starts:
						
						elem.html(elem.siblings('.questionData').html());
						
					},
					onEnd: function(){
						/* ====== Centering the answer after the flip ====== */
						var height = elem.find('.answer').height();
						var height = 72 - height;
						elem.find('.answer').css('margin-top', height / 2 + 'px');
						elem.find('.questcont').css('display', 'block' )
					}
				});
				
				// Setting the flag:
				elem.data('flipped',true);
			}
		});
	
		function isiOS(){
		    return (
		        (navigator.platform.indexOf("iPhone") != -1) ||
		        (navigator.platform.indexOf("iPad") != -1) ||
		        (navigator.platform.indexOf("iPod") != -1)
		    );
		}
		function play_audio(song_url) {
			//if ( song_url != '' && theme_path != '' ) {
				
				var song_url = song_url;
				var options = {
					soundFile: song_url,
					autostart: "yes",
					initialvolume: 100,
					transparentpagebg: 'yes'
				}
				audioElement = document.getElementById('audioHoversPlayer');
				if (!(!!(audioElement.canPlayType) && ("no" != audioElement.canPlayType("audio/mpeg")) && ("" != audioElement.canPlayType("audio/mpeg")))) {
					$(this).addClass('currently-playing');
					AudioPlayer.embed("audioHoversPlayer", options);
				} else {
					$('#audio-player').append('<audio id="audioHoversPlayer"></audio>');
					audioElement = document.getElementById('audioHoversPlayer');
					audioElement.setAttribute('src', song_url);
					$(audioElement).html('<source type="audio/mpeg" src="' + song_url + '">');
					if (isiOS()) {
						audioElement.load();
					}
					audioElement.play();
				}
			//}
			
		}
		$('.slider-holder ul li:gt(0)').hide();
		$(window).load(function(){
			if ( $(document).find('#custom-slider').length != 0 ) {
				
				var _slider_current_index = 0;
				setInterval(function () {
					var num_all_slides = $('.slider-holder li').length;
					var next_index = _slider_current_index == num_all_slides - 1 ? 0 : _slider_current_index + 1;
					$('.slider-holder li:eq(' + _slider_current_index + ')').fadeOut(1300);
					$('.slider-holder li:eq(' + next_index + ')').fadeIn(1300);
					
					 _slider_current_index = next_index;
				}, 5000);
			};
		})
	});
})(jQuery)
