/**
 * @author denfordberriman@gmail.com
 */
//fix for firebugless
if (window.console === undefined) {

	var ConsoleObject = function () {
		'use strict';
		this.time = function () {
		};
		this.timeEnd = function () {
		};
		this.group = function () {
		};
		this.groupEnd = function () {
		};
		this.dir = function () {
		};
		this.log = function () {
		};
	};
	window.console = new ConsoleObject();
}
/**  base url  **/
var get_url = window.location.href;
var hold_url = get_url.split("/");
var base_url = '';
var namee;
var email;
var message;

hold_url[2] = hold_url[2].replace('www.', '');
//alert(hold_url[2]);
if (hold_url[2] === 'tonybuntu' || hold_url[2] === 'localhost' || hold_url[2] === 'clone' || hold_url[2] === 'tachibuntu') {
	base_url = "http://" + hold_url[2] + "/boys-trip.co.nz/";
} else {
	base_url = "http://" + hold_url[2] + "/";
}
window.base_url = base_url;

//current url
var cururl = document.URL;

/**
 * 
 *  Flash Box
 * 
 */


/**
 * 
 * 
 *		Set Slider Label
 * 
 */
function slide_labels(ui, who) {
	'use strict';
	console.log(ui.value);
	console.log(who);
	var v = ui.value;
	if (v >= 0 && v < 10) {
		$(who).find('.rating_sign').find('.rating_mark').removeClass('rating-lit').addClass('rating-dim');
		$(who).find('.rating_sign').find('.rating1').removeClass('rating-dim').addClass('rating-lit');
	}
	if (v >= 11 && v < 20) {
		$(who).find('.rating_sign').find('.rating_mark').removeClass('rating-lit').addClass('rating-dim');
		$(who).find('.rating_sign').find('.rating2').removeClass('rating-dim').addClass('rating-lit');
	}
	if (v >= 21 && v < 40) {
		$(who).find('.rating_sign').find('.rating_mark').removeClass('rating-lit').addClass('rating-dim');
		$(who).find('.rating_sign').find('.rating3').removeClass('rating-dim').addClass('rating-lit');
	}
	if (v >= 41 && v < 60) {
		$(who).find('.rating_sign').find('.rating_mark').removeClass('rating-lit').addClass('rating-dim');
		$(who).find('.rating_sign').find('.rating4').removeClass('rating-dim').addClass('rating-lit');
	}
	if (v >= 61 && v < 80) {
		$(who).find('.rating_sign').find('.rating_mark').removeClass('rating-lit').addClass('rating-dim');
		$(who).find('.rating_sign').find('.rating5').removeClass('rating-dim').addClass('rating-lit');
	}
	if (v >= 81 && v <= 100) {
		$(who).find('.rating_sign').find('.rating_mark').removeClass('rating-lit').addClass('rating-dim');
		$(who).find('.rating_sign').find('.rating6').removeClass('rating-dim').addClass('rating-lit');
	}
	
}

$(function () {
	"use strict";
	//console.log('loaded. commence the js');
	
	
	$('#hearabout').live('click', function () {
		var hv = $('#hearabout').val();
		console.log(hv);
		if (hv === 'From another website, which one?') {
			$('#whichonebox').show();
			//$('#whichone').focus();  //seems to cause problems 
		} else {
			$('#whichonebox').hide();
		}
	});
	
	//flashbox
	$('#carousel_wrapper').cycle({
		speed:		1000,
		fx:			'fade',
		timeout:	5000,
		pause:		1,
		random:		0
	});
	
	//pretty Photos
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed: 'fast', /* fast/slow/normal */
		slideshow: 6000, /* false OR interval time in ms */
		autoplay_slideshow: true /* true/false */
	});
	
	//packages homepage
	$('.package').live('click', function () {
		var url = $(this).attr('id');
		window.location = url;
	});
	//sub-packages ajax
	$('.sub-package').live('click', function () {
		var url = $(this).attr('id');
		$('#package_stage').html("<div class='rel center'><img src='" + base_url + "images/big loader.gif' /></div>");
		$('.sub-package').css({'border': '2px solid #aaa'});
		$('#' + url).css({'border': '2px solid #f00'});
		$.ajax({
			type: "POST",
			url: base_url + "packages/subpackage/" + url,
			data: "name=John&location=Boston",
			success: function (data) {
				//alert( data );
				$('#package_stage').html(data);
			}
		});
	});
	
	$("#cal").datepicker();
	$('#cal').live('click', function () {
		console.log('alive');
	});
	
	$('#contact_stage').hide();
	//Contact Box
	$('#contact_send').live('click', function () {
		namee = $('#contact_box input[name=name]').val();
		email = $('#contact_box input[name=email]').val();
		message = $('#contact_box textarea').val();
		$.post(base_url + 'contact', {name: namee, email: email, message: message}, function (data) {
			$('#contact_box').html(data);
		});
		console.log('send contact for ' + name);
	}); 
	$('#contact_tab').live('click', function () {
		$('#contact_stage').slideToggle(400);
	});
	$('#contact_close').live('click', function () {
		$.post(base_url + 'contact', {}, function () {
			$('#contact_box').html();
		});
	});
	
	//switch tabs to quote
	$('.quote-button').live('click', function (event) { // bind click event to link
		event.preventDefault(); // cancel event
		$('#tabs').tabs('select', 1); // switch to third tab
		//jump to named anchor
		
		document.location = cururl + '#tabs-2';
		return false;
	});
		
	$(".slider").slider({
	
		create: function (event, ui) {
			var sv = $(this).parent().find('input[type=text]').val();
			$(this).slider({ value: sv });
			//alert(ui.value);
		},
		change: function (event, ui) {
			var who = $(this).parent();
			slide_labels(ui, who);
			//set the input
			//alert(ui.value);
			$(who).find('input[type=text]').val(ui.value);
		},
		start: function (event, ui) {
			var who = $(this).parent();
			//slide_labels(ui, who);
			//alert(ui.value);
		}
	});
	//set some default text and styles to comments
	$('.comments').each(function(index, element){
		var v = $(element).text();
		console.log('v = ' + v);
		if (v === '' || v === 'Your Comments') {
			$(this).text('Your Comments');
			$(this).css({'color':'#ddd'});
		}
	});
	$('.comments').live('click focus' , function () {
		var v = $(this).text();
		if (v === 'Your Comments') {
			$(this).text('');
			$(this).css({'color':'#333'});
			//console.log(index + '-' + element);
			console.log('red text');
		}
	});
	
	
	//open close comment-box
	$('.comment-box input[type=radio]').live('click', function () {
		var cv = $(this).val();
		if (cv === '0') {
			$(this).parent().parent().find('.comment-textarea').hide('slow');
		} else {
			$(this).parent().parent().find('.comment-textarea').show('slow');

		}
		//alert(cv);
	});
	//open close hearabout-box
	$('.comment-box select').live('change', function () {
		var cv = $(this).val();
		if (cv === 'other') {
			$(this).parent().find('.comment-textarea').show('slow');
		} else {
			$(this).parent().find('.comment-textarea').hide('slow');

		}
		//alert(cv);
	});
	
	
	
});

