(function($){$(function(){
  	
	$(".footerarticle").hover(function() {
		$(this).toggleClass('hover');
	});
	
	$(".tourholder .tour").hover(function() {
		$(this).toggleClass('hover');
	});
	
	/*$(".footerarticle a").hover(function() {
		$(this).parent().parent().find('a').toggleClass('hover');
	});*/
	
	/*** Footer Twitter Feed ***/
	$("#twitterfeed").tweet({
		username: "studio_online",
		join_text: "auto",
		avatar_size: 0,
		count: 2,
		auto_join_text_default: "",
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading tweets..."
	});
	
	
	/*** Header Teaser ***/
	var teaserDiv = document.getElementById('instructionimagesholder');
	if (teaserDiv != null){
		$('#cycleDiv').cycle({ 
			fx: 'fade',
			cleartype: 0,
			pause: 1,
			speed:4000
		});
	}
	
	/*** Login Popup ***/
	/*$( "#dialog" ).dialog({
		autoOpen: false,
		width: 860,
		height: 540,
		resizable: false
	});*/

	/*$( "#login a" ).click(function() {
		$('#dialog').html('');
		$('<iframe id="loginiframe" frameborder="0" width="460" height="460" />').attr('src', $(this).attr('href')).appendTo('#dialog'); 					 
		$( "#dialog" ).dialog("open");
		return false;
	});*/
	
	/*** Basic Panel accordion ***/
	$('.faqcategory h6').live('click', function() {
		$(this).parent().find('h6').removeClass('open');									
		$(this).parent().find('div.answer').slideUp('fast');
		$(this).next().slideToggle('fast');
		$(this).toggleClass('open');
		return false;
	});
	
	/*$('a.tourlink').each(function(i, val) {
		$(this).attr('href', $(this).attr('href') + 'ajax?ajax=1').attr('target','_blank');
	});

	$('a.tourlink').nyroModal({width: '863', height: '590', bgColor: '#ccc'});*/
	 
	/*$.nyroModalSettings({
		beforeShowLoad: function(elts, settings) {
			$('#nyroModalIframe').hide();
		},
		afterShowCont: function(elts, settings, callback) {
			$('#nyroModalIframe').show();
		}
	});*/
	 
	 
												
		/*										{
		
		$('#dialog').html('');
		$('<iframe id="touriframe" frameborder="0" width="840" height="500" />').attr('src', $(this).attr('href')+'ajax').appendTo('#dialog'); 					 
		$( "#dialog" ).dialog("open");		
		return false;
	});*/
	
	var tabContainers = $('div.tabs > div');
		tabContainers.hide().filter(':first').show();
		$('div.tabs ul.tabnav a').live('click',function(){
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				$('div.tabs ul.tabnav li').removeClass('selected');
				$(this).parent('li').addClass('selected');
				return false;
		}).filter(':first').click();

	/*** TourTab ***/
	function initTabs(){
		
		
	}
	
	$('#InstructionSteps li a').live('click', function(){
		var target = $(this);												  
		$.get('TourServicePage_Controller/getinstructionimages/' + $(target).attr('rel'), function(data){
			if(data){
				$('#InstructionSteps li.current').removeClass('current');
				$('#instructionimagesholder').cycle( 'destroy' );
				$('#instructionimagesholder').html(data);
				$(target).parent().addClass('current');
				initCycle();
			}
		});
	});
	
	/*** TourTab Service Cycle ***/
	function initCycle(){
		var cycleDiv = document.getElementById('instructionimagesholder');
		if (cycleDiv != null){
			$('#instructionimagesholder').cycle({ 
				fx:         	'fade', 
				timeout:    	5000,
				fit: 			1,
				//pager:      	'#pager', 
				//pagerEvent: 	'click', 
				fastOnEvent:	true,
				prev:			'#prev', 
				next:			'#next',
				slideExpr: 		'div.instructionslides'
				,after:			onAfter
			}).cycle('pause');
		}
	}
	
	function onAfter(curr, next, opts) {
		var index = opts.currSlide;
		//console.log(index);
		$('#InstructionListSteps ul li ').removeClass('current');
		$('#InstructionStep'+index).addClass('current');
	}

	$('#InstructionListSteps ul li').live('click',function(){
		$('#InstructionListSteps ul li').removeClass('current');
		$(this).addClass('current');
		var slide = parseInt($(this).attr('id').replace('InstructionStep',''));
		if(!isNaN(slide)){
			$('#instructionimagesholder').cycle(slide);
		}
	});
	
	//initTabs();
	initCycle();
	var referenceDiv = document.getElementById('referenceitems');
	if (referenceDiv != null){
		$('#referenceitems').cycle({ 
			fx:         	'fade', 
			timeout:    	5000,
			fit: 			1,
			fastOnEvent:	true,
			prev:			'#refprev', 
			next:			'#refnext',
			after:			onAfterTest
		});
	}
	function onAfterTest(curr, next, opts){
		//console.log(opts.currSlide);
	}
	
	
	/*** Price Calculation ***/
	
	function fadeImage(url){
		$('#priceimage img').fadeOut().attr('src', url).fadeIn();
	}
	
	function changeTotalPrice(val){
		var total = parseFloat($('#Form_PriceForm_PriceActionUnitPrice').val()) * val;
		$('#Form_PriceForm_PriceActionTotalPrice').val(parseFloat(total.toFixed(2)));
	};
	
	function getImageAndPrice(id){
		$.getJSON('TourPricePage_Controller/getimageandprice/'+id, function(data){
			if(data){
				if(data.price){
					var price = parseFloat(data.price);
					$('#PriceActionUnitPrice').html(price.toFixed(2));
					//var ammount = parseFloat($("#Form_PriceForm_Ammount").val());
					//var total = ammount * parseFloat(data.price);
					//$('#PriceActionTotalPrice').html(parseFloat(total.toFixed(2)));
				}
				if(data.image){
					fadeImage(data.image);
				}
			}
		});
	}
	
	function getPriceActionComplexities(id){
		$.getJSON('TourPricePage_Controller/getcomplexities/'+id, function(data){
			
			if(data.complexities){
				var comp = data.complexities;
				var options = '';
				for (var key in comp) {
					options += '<option value="' + key + '">' + comp[key] + '</option>';
				}
				$("#Form_PriceForm_PriceActionComplexities").html(options);
			}
																		   
																		   
			if(data.price){
				$('#PriceActionUnitPrice').html(data.price);
				//var ammount = parseFloat($("#Ammount").html());
				//var total = ammount * parseFloat(data.price);
				//$('#PriceActionTotalPrice').html(parseFloat(total.toFixed(2)));
			}
			if(data.image){
				fadeImage(data.image);
			}
			
		});
	}
	
	$("input[name=AmmountUp]").live("click", function(){
		var ammount = parseFloat($("#Form_PriceForm_Ammount").val())+1;
		$("#Form_PriceForm_Ammount").val(parseFloat(ammount.toFixed(2)));
		changeTotalPrice(ammount);
	});
	
	$("input[name=AmmountDown]").live("click", function(){
		var ammount = parseFloat($("#Form_PriceForm_Ammount").val())-1;
		if(ammount < 1){ammount = 1;}
		$("#Form_PriceForm_Ammount").val(ammount.toFixed(2));
		changeTotalPrice(ammount);
	});
	
	$('#Form_PriceForm_Ammount').change(function(){
		var ammount = parseFloat($(this).val());
		if(isNaN(ammount)){
			ammount = 1;
			$(this).val(ammount);
		}
		changeTotalPrice(ammount);
	});
	
	$("#Form_PriceForm_PriceActionComplexities").change(function(){
		getImageAndPrice($(this).val());														 
	});
	
	$("#Form_PriceForm_PriceActionTypes").change(function(){
		getPriceActionComplexities($(this).val());														 
	});	
	
	
});})(jQuery);
