
var mp3Goes = '';

$( function() {
	

	// mp3 player
	
	$('a.player').click(function(event) {
		var trackId = $(this).attr('title');
		var $button = $(this);
		if (  mp3Goes ) {			
			$('.playback').html('play');
			$('.playback').removeClass('playback');
			$('#JPlayer').jPlayer('clearMedia');
			$('#JPlayer').jPlayer('destroy');			
		}
		$.ajax({
			url: '/helpers/getmp3.php',
			cache: false,
			type: 'POST',
			dataType: 'html',
			data : {
				'track_id' : trackId			
			},			
			error : function(){
				
			},
			success : function( data ) {
				if ( data ) {
					var href = data;					
					if ( mp3Goes ) {
						/*
						$('#JPlayer').jPlayer('stop');
						$('#JPlayer').jPlayer('destroy');				
						$('.player').html('play');
						$('.player').removeClass('playback');
						*/
						
						if ( mp3Goes != href ) {
							$button.html('stop');		
							$button.addClass('playback');
							mp3Goes = href;
							$('#JPlayer').jPlayer( {
							    ready: function () {
							      $(this).jPlayer("setMedia", {
							        mp3: href 
							      }).jPlayer("play"); // Attempts to Auto-Play the media
							    },		 
							    swfPath: "/web/js"
							  });
						} else {
							mp3Goes = '';
						}
					} else {
						// play
						$button.html('stop');
						$button.addClass('playback');
						mp3Goes = href;
						$('#JPlayer').jPlayer( {
						    ready: function () {
						      $(this).jPlayer("setMedia", {
						        mp3: href 
						      }).jPlayer("play"); // Attempts to Auto-Play the media
						    },		 
						    swfPath: "/web/js"
						  });
					}
				} 					
			}
		});
	
	
		//var href = $(this).attr('href');
		
		event.preventDefault();
	});
	
	// Image Switcher	
	$("#cmsImages img").hide();
	$("#cmsImages img").first().addClass('active');
	$("#cmsImages img").first().show();
	
	$(".imagesSwitcher .next").click( function(e) {		
		
		$('#cmsImages img.active').each( function() {		
			
			$(this).fadeOut();
			$(this).removeClass('active');
			
			if ( $(this).next().length ) {				
				$(this).next().fadeIn();
				$(this).next().addClass('active');
			} else {
				$(this).siblings().first().fadeIn();
				$(this).siblings().first().addClass('active');
			}
			
		});
	});
	
	$('.monthSelector ul#Months .active').each( function() {
		
		var $month = $(this).attr('id');
		$month = $month.replace('month_','');
		$('ul#tours li').each( function() {
			var $class = 'month_'+$month;			
			if ( !$(this).hasClass( $class ) ) $(this).hide();
			else $(this).show();
		});
	});
	
	$('.monthSelector ul#Months li').click( function() {
		$('.monthSelector ul#Months li').each( function() { $(this).removeClass('active')});
		$(this).addClass('active');
		var $month = $(this).attr('id');
		$month = $month.replace('month_','');
		$('ul#tours li').each( function() {
			var $class = 'month_'+$month;			
			if ( !$(this).hasClass( $class ) ) $(this).hide();
			else $(this).show();
		});
	});
	
	$(".imagesSwitcher .previous").click( function(e) {
		
		$('#cmsImages img.active').each( function() {		
			
			$(this).fadeOut();
			$(this).removeClass('active');
			
			if ( $(this).prev().length ) {				
				$(this).prev().fadeIn();
				$(this).prev().addClass('active');
			} else {
				$(this).siblings().last().fadeIn();
				$(this).siblings().last().addClass('active');
			}			
		});		
	});
	
	
	
	$("table.sq_table").treeTable( { initialState: 'expanded' } );
	
	
	// lightbox
	$("a.fancyBox").colorbox({
		photo: true,		
		opacity: 0.65,
		scalePhotos: true,		
		maxWidth: '75%', 
		maxHeight:'75%'	
	});
	
	
	// lightbox
	$("a.textBox").colorbox({		
		html: true,
		width: 640,
		height: 600,
		opacity: 0.65,		
		iframe: true,
		maxWidth: '95%', 
		maxHeight:'95%'
	});
	
	// ui calendar
	$('input.datePicker').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'dd/mm/yy',
		showOn: 'button',
		buttonImage: '/images/btn_calendar.gif',
		buttonImageOnly: true,
		autoSize: true,
		buttonText: 'Select a date from a calendar',
		yearRange: 'c-90:c+10'
	});

	
	
	// Image Cropper
	$('.cropbox').Jcrop({
		aspectRatio: 0,
		onSelect: function(c, obj) {
			
			$(obj).parent().find('input.crop_dimentions').val( c.x + ',' + c.y + ',' + c.w + ',' + c.h );
			if ( c.w > 0 ) {
				$(obj).parent().find('input.submitCrop').each( function() {
					$(this).attr("disabled", false);
				});
			} else {
				$(obj).parent().find('input.submitCrop').each( function() {
					$(this).attr("disabled", true );
				});
			}
		}
	});
	
	$('.submitCrop').click( function(e) {
		if ( confirm('Are you sure ? To revert to original size you would need to reupload the image.') ) {
			$('input').each( function() {
				var $this = $(this);						
					
				if ( $(this).attr('name') == 'refresh_form' ) {					
					$this.val('true');					
				}
			});
		} else {
			return false;
		}
	});

	
	
	//quick popup login form 
	$('a.loginLink').bind('click',function(e) {
		
		e.preventDefault();
		$(this).addClass('on');
		
		var $this = $(this);  
				
		var $usernameField = $('<div></div>',{
			'class' : 'field'
		}).append( $('<label></label>',{
			'for': 'username',
			html : 'Username'
		}).after( $('<input></input>',{
			'id' : 'username',
			'type' : 'text',
			'name' : 'users[USER][username]'
		}) ));
		
		var $passwordField = $('<div></div>',{
			'class' : 'field'
		}).append( $('<label></label>',{
			'for': 'password',
			html : 'Password'
		}).after( $('<input></input>',{
			'id' : 'password',
			'type' : 'password',
			'name' : 'users[USER][password]'
		}) ));
		
		var $submitButton = $('<input></input>',{
			'type' : 'submit',
			'class' : 'submit',
			'value' : 'Login'
		});
		
		var $cancelButton = $('<input></input>',{
			'type' : 'submit',
			'class' : 'submit',
			'value' : 'Cancel',
			click : function(e) {
				e.preventDefault();
				$('#quickLoginBox').fadeOut('fast',function(){
					$this.removeClass('on');
					$(this).remove();
				});
			}
		});
		
		$(this).parent().prepend( $('<div></div>',{
			'id' : 'quickLoginBox'
		})
		.fadeIn()
		.append( $('<form></form>',{
			submit: function(e){
				e.preventDefault();
				
				// remove error message
				$('#quickLoginBox').find('div.error').fadeOut(function(){$(this).remove()});
								
				var usernameValue = $(this).find('input#username').val();
				var passwordValue = $(this).find('input#password').val();
				
				$.ajax({
					url: '/helpers/quickLogin.php',
					cache: false,
					type: 'POST',
					dataType: 'json',
					data : {
						'username' : usernameValue,
						'password' : passwordValue
					},			
					error : function(){
						// redirect to admin area for standard login
						window.location.replace("/admin");
					},
					success : function(data, textStatus) {
						if ( data.error ) {
							$('#quickLoginBox').prepend( $('<div></div>',{
								'class' : 'error',
								html: data.error
							}));
							$(':input[type=password]','#quickLoginBox').each(function(){
								$(this).val('');
							});
						} else if ( data.success ) {
							// should be logged in now so refresh the page
							window.location = self.location;
						}						
					}
				});
				return false;
			}
		})
		.append( $('<fieldset></fieldset>')
			.append($usernameField)
			.append($passwordField)
			.append($submitButton)
			.append($cancelButton)
			)
		));
	});
	
	
		
});


/**
 * !important!
 * if you change something here remember to copy it to
 * tinyMCE part in the inline editor above
 */  
tinyMCE.init({
	theme_advanced_resizing_min_width : 550,
	mode : "specific_textareas",
	editor_selector : /(tiny_mce|image_mce)/,
	theme : "advanced",
	editor_deselector : "mceNoEditor",
	body_class : "cms",
	content_css : "/css/content.css",
	plugins: "safari,inlinepopups,paste,table",
    paste_auto_cleanup_on_paste : true,
	width: "640",
	height: "500",
	theme_advanced_buttons1 : "bold, italic, underline, justifyleft,justifycenter, justifyright, justifyfull , formatselect, fontsizeselect,  |, pasteword,pastetext, code ",
	theme_advanced_buttons2 : "undo,redo,|,bullist, numlist,|,outdent,indent, |, link, unlink, image ,forecolor",
	theme_advanced_buttons3 : "tablecontrols",
	theme_advanced_resizing : true,
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "center",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_blockformats : "p,h2,h4",
	convert_urls : false,
	verify_html : false,
	valid_elements : 
		"@[class|style|title],br,h2,h3,h4,-p[align],-strong/b,a[name|href|target],"+
		"img[src|border=0|alt|title|width|height|align],hr[width|size|noshade],"+
		"table[border|cellspacing|cellpadding|width],tr[rowspan],td[colspan],th[colspan],-ul,ol,-li"
});

