jQuery(function(){
	jQuery('.sf-menu').superfish();
});

$(document).ready(function(){
	// $('#rotating').find('#yahoomail.macbook').removeClass('macbook').addClass('ipad');
	$('#rotating').blockSlider();
	$('.rotating_container').css('width', '100%');
	$('.rotating_container li').css('width', '100%');
	
	$('#approach-content').bxSlider({
		mode: 'slide',
		speed: 500,
		pause: 5000, 
		width: 365,
		auto: false,
		controls: true,
		auto_direction: 'left',
		next_text: '',
		prev_text: '',
		wrapper_class: 'rotating_container'
	});
	
	$('#services-index').bxSlider({
		mode: 'slide',
		speed: 1000,
		pause: 8000,
		auto: false,
		controls: true,
		auto_direction: 'left',
		next_text: '',
		prev_text: '',
		wrapper_class: 'rotating_container'
	});
	
	$(".getting").fancybox({
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'autoScale'     	: false,
		'overlayColor' : '#143042',
		'overlayOpacity' : 0.65,
		'padding' : 0,
		'onComplete' : function() {
			$(".close").click($.fancybox.close);
		}
	});
	
	$(".getting").click(function(){
		$('.step').hide();
		$('#step1').fadeIn('slow');
		$("#phone_or_email_value").val('');
		$("#when_value").val('');
		$("#name").val('Full Name');
		$(".checkbox").attr('checked', false);
		$('#step1-errors-wrapper').html('');
		$('#step2-errors-wrapper').html('');
		$(".textbox").removeClass('textbox-error');
		$("#proposal").val('');
	});
	
	
	$("#request-quote").fancybox({
		'width' : 670,
		'height' : 484,
		'autoScale' : false,
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'type' : 'iframe'
	});
	
	$("#next-step").click(function(){
		var value = $('.checkbox:checked').val();
		if(value){
			if(value == 'Proposal_Quote' && !document.getElementById('proposal').value){
				$('#step1-errors-wrapper').html('For Proposal/Quote, please upload a file');
				$('#step1-errors-wrapper').css('line-height', '1.5');
			}
			else{
				$('#step2').fadeIn('slow');
				$('#step1').fadeOut('fast');
			}
		}else{
			$('#step1-errors-wrapper').html('Please select an option');
			$('#step1-errors-wrapper').css('line-height', '2');
		}
	});
	
	$.fn.setDisable = function() {
   	if($('input[name=when]:checked').val() == 'asap'){
			$("#when_value").attr("disabled", "disabled");
			$("#when_value").css("display", "none");
			$("#time").attr("disabled", "disabled");
			$("#time").css("display", "none");
		}
		else{
			$("#when_value").removeAttr("disabled");
			$("#when_value").css("display", "block");
			$("#time").removeAttr("disabled");
			$("#time").css("display", "block");
		}
	};
	
	$('input[name=when]').click(function(){
		$(this).setDisable();
	});
	
	$.fn.clearDefault = function(){
		return this.each(function(){
			var default_value = $(this).val();
			$(this).focus(function(){
				if ($(this).val() == default_value){
					$(this).val("");
					$(this).css('color', '#000');
				}
				$(this).removeClass("textbox-error");
			});
			$(this).blur(function(){
				if ($(this).val() == ""){
					$(this).val(default_value);
					$(this).css('color', '#a9a9a9');
				}
			});
			this.setAttribute("autocomplete", "off");
		});
   };

	$('input[name=when]').setDisable();
	$('#when_value').datepicker({inline: true});
  $('.textbox').clearDefault();
	
	$('#getting-overlay').submit(function(event){
		var name = $("#name").val();
		name = (name == 'Full Name') ? '' : name; 
		var email = "";
		var phone = "";
		if($('input[name=phone_or_email]:checked').val() == 'Email')
			email = $("#phone_or_email_value").val();
		else
			phone = $("#phone_or_email_value").val();
		var when = $("#when_value").val();
		name ? $("#name").removeClass('textbox-error') : $("#name").addClass('textbox-error');
		email || phone ? $("#phone_or_email_value").removeClass('textbox-error') : $("#phone_or_email_value").addClass('textbox-error');
		if($('input[name=when]:checked').val() == 'pick')
			when ? $("#when_value").removeClass('textbox-error') : $("#when_value").addClass('textbox-error');
		if($(".textbox").hasClass('textbox-error')){
			$('#step2-errors-wrapper').html('Required fields are not filled in');
			return false;
		}
		if(email && !email.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)){
			$("#phone_or_email_value").addClass("textbox-error");
			$('#step2-errors-wrapper').html('Email format is invalid');
			return false;
		}
		if(phone && !phone.match(/^[0-9 ():.ext,+-]{7,16}$/)){
			$("#phone_or_email_value").addClass("textbox-error");
			$('#step2-errors-wrapper').html('Phone number format is invalid');
			return false;
		}
		dataString = 'ajax=1&name=' + name;
		dataString += email ? '&email='+ email : '&phone=' + phone;	
		dataString += when ? '&when=' + when + '&time=' + $("#time").val() : '&when=asap';
		if($("#proposal").val())
			dataString += '&proposal=' + $("#proposal").val();
		$('.checkbox:checked').each(function(i){
			dataString += '&' + $('.checkbox:checked').attr('id') + '=' + $(this).val();
		});
		$('#step3').fadeIn('slow');
		$('#step2').fadeOut('fast');
		$('#response').html('<span class="success">Sending...</span>');
		$.ajax({ type: "GET", url: "http://" + document.domain + "/wp-content/themes/freshout/send_getting_started_request.php?" + dataString, context: document.body, success: function(data){
				if (data.match('success')){
					$('#step4').fadeIn('slow');
					$('#step3').fadeOut('fast');
				}
				else{
					$('#step4 > div:first-child').html(data);
					$('#step4').fadeIn('slow');
					$('#step3').fadeOut('fast');
				}
      }});
		event.preventDefault();
	});
	
		
	jQuery(window).bind('load', function(){
        $('#right-side').css('height', $('#cat_0').height());		
		var margin = parseInt($('.content-heading').css('margin-top'));
		margin += parseInt($('.content-heading').css('margin-bottom'));
		if ( $('.content-heading').parents('.work-content-details') ) {
			return false
		} else {
			$('.content-heading').css('min-height', $(window).height() - 763 - margin);	
		}
		$('.jobs').css('min-height', $('.category:first-child').height());
	});

	jQuery(window).bind('resize', function(){
		if ($(window).height() > $('.content-heading').height()){
			var margin = parseInt($('.content-heading').css('margin-top'));
			margin += parseInt($('.content-heading').css('margin-bottom'));
			if ( $('.content-heading').parents('.work-content-details') ) {
				return false
			} else {
				$('.content-heading').css('min-height', $(window).height() - 763 - margin);	
			}
		}
	});
	
	/******* Services Page *********/
	var tmp_pos = false;
	var scroll_services = $("#service-description").scrollable({
		size       : 1,
		speed      : 300,
		loop       : true,
		items      : "#services",
		prev       : "a#desc-prev",
		next       : "a#desc-next",
		easing     : "linear",
		api: true,
		onSeek: function() { 
			$('.service-column a').removeClass('selected');
			$('.service-column a').parent('li').removeClass('selected');
			$("a[name='service"+this.getPageIndex()+"']").addClass('selected');
			$("a[name='service"+this.getPageIndex()+"']").parent('li').addClass('selected');
			if (!first_time && tmp_pos == 0 && this.getIndex() == 0) {this.end();};
			first_time = false;
			var serv_id = $("a[name='service"+this.getPageIndex()+"']").parents('.service-column').attr('id');
			if(serv_id == 'serv-st')
				$('.indicator').css('left', '82px');
			else if(serv_id  == 'serv-nd')
				$('.indicator').css('left', '310px');
			else if(serv_id  == 'serv-rd')
				$('.indicator').css('left', '573px');	
			else
				$('.indicator').css('left', '809px');
			return false;  
		}, 
		onBeforeSeek: function() {
			tmp_pos = this.getIndex();
		},
		keyboard   : false
	});
	
	$('.service-column').each(function(){
		var scontainer = this;
		var destination = $('.services').offset().top;
		$('a', scontainer).each(function(){
			$(this).bind('click', function(){
				if ($(this).hasClass('selected')) return false;
				$('.service-column a, .service-column li').removeClass('selected');
				$(this).addClass('selected');
				$(this).parent('li').addClass('selected');
				if(scontainer.id == 'serv-st')
					$('.indicator').css('left', '82px');
				else if(scontainer.id == 'serv-nd')
					$('.indicator').css('left', '310px');
				else if(scontainer.id == 'serv-rd')
					$('.indicator').css('left', '573px');	
				else
					$('.indicator').css('left', '809px');
				$(scontainer).addClass('active');	
				//$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 300 );
				scroll_services.setPage($(this).attr('name').split('service')[1]);
				return false;
				//window.location.hash = $(this).attr('name');
				//return false;
			});

		});
	});
	
setTimeout(function(){
    var theUrl = document.location.toString();
	if (theUrl.match('/services/') || theUrl.match('page_id=145')) {
		makeSlug = function(str) {
			var slugcontent_hyphens = str.replace(/\s/g,'');
			var finishedslug = slugcontent_hyphens.replace(/[^a-zA-Z0-9\-]/g,'');
			return finishedslug.toLowerCase();
		}
		$('.service-column li a').each(function() {
			$(this).attr('name', 'service' + $(this).attr('href').split('#')[1]);
			$(this).attr('href', '#' + makeSlug($(this).attr('title')));
		});
		var itmCnt = theUrl.split('#').length - 1;
		var slug;
		first_time = true;
		if (itmCnt > 0) {
			if (itmCnt > 1) {
				slug = theUrl.split('#')[2];
				$('a[href="#'+slug+'"]').click();
			} else {
				slug = theUrl.split('#')[1];
				if(slug != '')
					$('a[href="#'+slug+'"]').click();
			}
		}
		
		$('.opt-capabilities > .level-1 a').click(function() {
			var hash = $(this).attr('href').split('/services/')[1];
			$('a[href="'+hash+'"]').click();
			return false;
		});
	}
}, 500);	
	
    $.getJSON("http://search.twitter.com/search.json?callback=?&q=from%3Afreshchris+http&show_user=true&rpp=3", function(json){
	    $.getJSON("http://search.twitter.com/search.json?callback=?&q=from%3Anavidsafa+http&show_user=true&rpp=3", function(json2){
		    var tweets = '';	        
	        json.results = json.results.concat(json2.results);
            if(json.results.length == 0){       
                $('#tweets-footer').css('display', 'block');
                return false;              
        	}
            json.results.sort(function(a, b){
                return a.created_at < b.created_at;
            });                                                                                                      
		    $.each(json.results,function(i,tweet){
			    var lastTweet =  tweet.text;
			    var pattern = /(HTTP:\/\/|HTTPS:\/\/)([a-zA-Z0-9.\/&?_=!*,\(\)+-]+)/g;
			    lastTweet = lastTweet.replace(/(https?:\/\/[\w\-:;?&=+.%#\/]+)/gi, '<a href="$1">$1</a>');
			    var rol = (tweet.from_user == 'navidsafa') ? 'VP of production' : 'General Manager';
			    lastTweet = '<a href="http://www.twitter.com/' + tweet.from_user + '" class="author" target="_blank">' + tweet.from_user + '</a><span class="rol">, ' + rol + ' </span><br/>' + ' ' + lastTweet;
			    lastTweet = '<p><span class="avatar"><img src="' + tweet.profile_image_url + '"/></span>' + lastTweet + '</p>';
			    tweets += "<li class='tweet'>" + lastTweet + "</li>";
		    });
			$('#tweets-footer').html(tweets).css('display', 'block');
			if(json.results.length == 1){
				return false;            
			}
			$('#tweets-footer').bxSlider({
				mode: 'fade',
				speed: 1000,
				pause: 6000,
				auto: true,
				controls: false,
				auto_direction: 'left',
				next_text: 'next image',
				prev_text: 'previous image',
				wrapper_class: 'rotating_container'
			});
			$('a').each(function() {                                                                                                                                
           		if(this.href && !new RegExp(location.host, 'gi').test(this.href) && !new RegExp('mailto', 'gi').test(this.href) && (this.href != 'http://twitter.com/') && (this.href != 'http://facebook.com/')) {
           			$(this).pageSlide({height:'80%', modal: true});  
           			Elastic.refresh();
           		}
           	});	
		});
	});
	
	
	
	/****** Jobs Page *****/
	$('.toggle').each(function(){
		var toggle = this;
			$(this).bind('click', function(){
				$('.toggle-desc', toggle).slideToggle(100, function(){
                    if($('.toggle-show div', toggle).css('backgroundPosition') == '0px -21px' || $('.toggle-show div', toggle).css('backgroundPosition') == '0% 0%'){
						    $(this).addClass('active');
						    $('.toggle-title', toggle).addClass('tt-active');
						    $('.toggle-show div', toggle).css('backgroundPosition', '0px -42px');
					}else{    
						    $(this).removeClass('active');
						    $('.toggle-title', toggle).removeClass('tt-active');   
						    $('.toggle-show div', toggle).css('backgroundPosition', '0% 0%');
					}
				});
			});
			$(this).hover(function(){                   
			    
				if(!$('.toggle-desc', toggle).hasClass('active')){
					$('.toggle-show div', toggle).css('background-position', '0px -21px');
					$('.toggle-title', toggle).addClass('tt-active');
				}
			});
			$(this).mouseleave(function(){
				if(!$('.toggle-desc', toggle).hasClass('active')){
					$('.toggle-show div', toggle).css('background-position', '0% 0%');
					$('.toggle-title', toggle).removeClass('tt-active');
				}
			});
	});

	$.fn.cityClick = function(){
		$('.cities li a').removeClass('selected');
		$(this).addClass('selected');
		$('.category').hide();
		var container = '.' + $(this).attr('id');
		$(container).show();
  };

	$('.cities li a').click(function(){
		$(this).cityClick();
	});
	
	$('#partlinks a').click(function(){
		var theID = $(this).attr('href');
		$('#partlinks a').css('color', '#248DD3');
		$('#right-side').css('height', $(theID).height());
		$(this).css('color', '#aaaaaa');
		$('.section').fadeOut('slow');
			
			$(theID).fadeIn('slow');
			return false;
	});           
	
	$('#request-info').hover(function(){
		$('.schedule').css('background-position', '0 100%');
	});
	$('#request-info').mouseleave(function(){
		$('.schedule').css('background-position', '0 0');
	});
	
});

jQuery(function(){                    
	new AjaxUpload('test1', {
		action: 'upload-file.php',
			onChange: function(file, extension){
				$('#opt2').attr('checked', 'checked');
				$('#step1-errors-wrapper').html("<span style='color: #02B2FC'>Uploading file...</span>");
			},
			onComplete: function(file, response){                        
				if (response.match('Error')){
					$('#step1-errors-wrapper').css('line-height', '1');
					$('#step1-errors-wrapper').html(response);
				}
				else{
					$('#step1-errors-wrapper').html("<span style='color: #02B2FC'>File Uploaded Successfully</span>");
					$('#step1-errors-wrapper').css('line-height', '1.5');
					$("#proposal").val(response);
				}
			}
		});
 });
