/**** Add application wide javascripts below this point  ******/

/**** window load - has to be window load due to safari & needing to manipulate css ****/
$(window).load(function(){	
	
	/*hide the forms on the edit page*/
	if($('.hidden_form').length){
		$('.hidden_form').hide();
	}
	/*on click show them*/
	if($('.operation .clickable').length){
		$('.operation .clickable').click(function(){
			$(this).next('.hidden_form').slideToggle("fast");
		});
	}
	/*show advanced options*/
	if($('h3.advanced_clickable').length){
		$('h3.advanced_clickable').click(function(){
			$('table.advanced').slideToggle("fast");
		});
	}
	
	/*are you sure confirmation*/
	if($('.areyousure').length){
		$('.areyousure').click(function(){return confirm('Are You Sure??');});
	}
	if($('.areyousure_form').length){
		$(".areyousure_form input[type='submit']").click(function(){return confirm('Are You Sure??');});
	}
	
	/*deploy confirmation warning*/
	if($('a.deploy').length){
		$('a.deploy').click(function(){return confirm('Are you sure you want to deploy your flake?');});
	}
	/*un hide the js*/
	if($('.hidden_js').length){
		$('.hidden_js').removeClass('hidden_js');
	}
	
	/*on the step by step flake adding, some clever stuff */
	if($('.has_help').length){
		/*on hover of a has_help area highlight the help div*/
		$('.has_help').hover(
			function(){
				segment = $(this).attr('id');
				$('.help').removeClass('highlight');
				$('.'+segment).addClass('highlight');
			},
			function(){
				segment = $(this).attr('id');
				$('.help').removeClass('highlight');
				$('.'+segment).removeClass('highlight');
			});
	}
	/*on the step by step form look for errors and higlight help box*/
	if($('span.error_message').length){
		$('span.error_message').each(function(){
			segment = $(this).parent().attr('id');
			$('div.'+segment).addClass('error_message');
		});
	}
	
	/*modal boxes*/
	if($('a.modal_click').length){
		$("a.modal_click").modal();
	}
	
	/*tooltip*/
	if($('.tooltip').length){
		$('.tooltip').each(function(){
			id = $(this).attr('id');
			if($('div.'+id+'_help').length){
				$('#'+id).hover(
					function(e){
						div = $(this).attr('id');						
						max = get_width() - 10;
						pos = e.pageX;
						if((pos+185) > max){
							left = max - 185;
						}else {
							left = pos;
						}
						$('div.'+div+'_help').addClass('show_tooltip');
						$('div.'+div+'_help').css('top', (e.pageY+20) + "px");
						$('div.'+div+'_help').css('left', left + "px");
					}, 
					function(){
						div = $(this).attr('id');
						$('div.'+div+'_help').removeClass('show_tooltip');
					}
				);
			}
		});
	}
	
});

function get_width(){
	if( typeof( window.innerWidth ) == 'number' ) {
  	return window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
		return document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
		return document.body.clientWidth;
	}
}



