// always remove outlines
$("a").each(function(){
	this.onmouseup = this.blur();
});
$(document).ready(function(){
	// for debugging only
	// console.log($.fn.$);

	// show / hide pages
	$('.hidepage').hide();
	$('.pagebtn').click(function() {
		$('.hidepage').hide();
		$('.hidepage1').hide();
		$( '#page' + $(this).html() ).fadeIn('slow');
		return false;
	});
	// show all pages
	$('.allpagesbtn').click(function() {
		$('.hidepage').show('slow');
		$('.hidepage1').show('slow');
		return false;
	});

	// design page - jquery ui tabs
	$(function() {
		$("#tabs").tabs();
	});

	// uniform form styles
	$(":text, select, :radio, :checkbox").uniform();

	// quote form budget slider
	$("#budget_slider").slider({
		value:0,
		min: 300,
		max: 10000,
		step: 100,
		slide: function(event, ui) {
			if(ui.value == 0) {
				$("#project_budget").val('Please Select...');
			} else {
				$("#project_budget").val('$' + ui.value);
			}
		}
	});
	$("#project_budget").val('Please Select...');

	// iphone style checkboxes
	$('.form :checkbox').iphoneStyle({
	  checkedLabel: 'YES',
	  uncheckedLabel: 'NO'
	});

	// expanding text area
	$('textarea').elastic();

	// validate the contact and payment forms
//	$("#contactform").validate();

	// prevent double-clicking form submit
	$("form").submit(
	  function() {
		$("input.submit").attr("disabled","true");
	  }
	);

	// fade info div
//	setTimeout(function(){
//		$("div.fadeinfo").fadeOut("slow", function () {
//			$("div.fadeinfo").remove();
//		});
//	}, 6000);

	// menu
	//for more transitions, goto http://gsgd.co.uk/sandbox/$/easing/
//	var style = 'easeOutElastic';
	//Retrieve the selected item position and width
//	var default_left = Math.round($('#menu li.selected').offset().left - $('#menu').offset().left);
//	var default_css_width = $('#menu li.selected').width();
	//Set the floating bar position and width
//	$('#box').css({left: default_left});
//	$('#box .head').css({width: default_css_width});
	//if mouseover the menu item
//	$('#menu li').hover(function () {
		//Get the position and width of the menu item
//		left = Math.round($(this).offset().left - $('#menu').offset().left);
//		width = $(this).width();
//	$('#box').html(left);
		//Set the floating bar position, width and transition
//		$('#box').stop(false, true).animate({left: left},{duration:1000, easing: style});
//		$('#box .head').stop(false, true).animate({width:width},{duration:1000, easing: style});
	//if user click on the menu
//	}).click(function () {
		//reset the selected item
//		$('#menu li').removeClass('selected');
		//select the current item
//		$(this).addClass('selected');
//	});
	//If the mouse leave the menu, reset the floating bar to the selected item
//	$('#menu').mouseleave(function () {
		//Retrieve the selected item position and width
//		default_left = Math.round($('#menu li.selected').offset().left - $('#menu').offset().left);
//		default_css_width = $('#menu li.selected').width();
		//Set the floating bar position, width and transition
//		$('#box').stop(false, true).animate({left: default_left},{duration:1500, easing: style});
//		$('#box .head').stop(false, true).animate({width:default_css_width},{duration:1500, easing: style});
//	});
//	$("#menu").disableSelection();

	// rollovers ------------------------------- //
	$('#home-facebook').hover(
		function(){ // Change the input image's source when we "roll on"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/home_facebook_over.png'});
		},
		function(){ // Change the input image's source back to the default on "roll off"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/home_facebook.png'});
		}
	);
	$('#home-twitter').hover(
		function(){ // Change the input image's source when we "roll on"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/home_twitter_over.png'});
		},
		function(){ // Change the input image's source back to the default on "roll off"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/home_twitter.png'});
		}
	);
	$('#home-linkedin').hover(
		function(){ // Change the input image's source when we "roll on"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/home_linkedin_over.png'});
		},
		function(){ // Change the input image's source back to the default on "roll off"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/home_linkedin.png'});
		}
	);
	$('#learn-more-hosting').hover(
		function(){ // Change the input image's source when we "roll on"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/learn-more_over.jpg'});
		},
		function(){ // Change the input image's source back to the default on "roll off"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/learn-more.jpg'});
		}
	);
	$('#learn-more-design').hover(
		function(){ // Change the input image's source when we "roll on"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/learn-more_over.jpg'});
		},
		function(){ // Change the input image's source back to the default on "roll off"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/learn-more.jpg'});
		}
	);
	$('#view-blog').hover(
		function(){ // Change the input image's source when we "roll on"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/view-blog_over.jpg'});
		},
		function(){ // Change the input image's source back to the default on "roll off"
			$(this).attr({ src : '/wp-content/themes/websightdesigns/images/view-blog.jpg'});
		}
	);

	// easy slider portfolio
	$("#slider").easySlider({
		loop: true,
		orientation: '',
		autoplayDuration: 2000,
		autogeneratePagination: false,
		restartDuration: 6000,
		prevId: 'prevBtn',
		nextId: 'nextBtn',
		prevText: 'Previous',
		nextText: 'Next',
		autogeneratePagination: false,
		pauseable: false,
		speed: 500
	});

	// all external links in a new window
	var h = window.location.host.toLowerCase();
	$("a[href^='http']:not(.external-image):not(.sociable):not([href^='http://" + h + "']):not([href^='http://www." + h + "']):not([href^='https://" + h + "']):not([href^='https://www." + h + "']):not([href^='http://mail.websightdesigns.com']):not([href^='mailto']), a[href$='.pdf']").attr("target", "_blank").append(' &raquo;');
	$("a[href^='http://www.websightdesigns.com/demos/']:not(.external-image):not(.sociable):not([href^='mailto']), a[href$='.pdf']").attr("target", "_blank").append(' &raquo;');

	// new window on click for external links
	$('a.external')
		.attr({
		target: "_blank"
	}).append(' &raquo;');

	// new window on click for external links, with title tag
	$('a.external-title')
		.attr({
		target: "_blank",
		title: "Open In A New Window"
	}).append(' &raquo;');

	// new window for images
	$('a.external-image')
		.attr({
		target: "_blank"
	});

	// new window for social icons
	$('a.sociable')
		.attr({
		target: "_blank"
	}).remove(' &raquo;');

	// lightbox
	$(function($) {
	    $(".lightbox").lightbox({
		fitToScreen: true,
		imageClickClose: false
	    });
	});

	// date picker
	$.datepicker.setDefaults({ dateFormat: 'yy-mm-dd' });
	$(function($) { $("#ticket_due").datepicker({minDate: '+2D'}); });
	$(function($) { $("#duedate").datepicker({minDate: '+2D'}); });
	$(function($) { $("#milestone_duedate").datepicker({minDate: '+2D'}); });
	$(function($) { $("#invoicedate").datepicker(); });
	$(function($) { $("input[name=duedate]").datepicker({minDate: '+2D'}); });
	$(function($) { $("input[name=invoicedate]").datepicker(); });

	// show/hide payments fields
	$('#payment_invoice').hide();
	$('#select_invoice').click(function() {
		$('#payment_invoice').show();
		$('#payment_hosting').hide();
		return false;
	});
	$('#select_hosting').click(function() {
		$('#payment_hosting').show();
		$('#payment_invoice').hide();
		return false;
	});

	// show/hide overdue/upcoming dashboard panels
	$('#dashboard_upcoming').hide();
	$('#dashtext_upcoming').hide();
	$('#dashlink_overdue').hide();
	$('#dashlink_upcoming').click(function() {
		$('#dashboard_overdue').hide();
		$('#dashtext_overdue').hide();
		$('#dashlink_upcoming').hide();
		$('#dashboard_upcoming').fadeIn();
		$('#dashtext_upcoming').fadeIn();
		$('#dashlink_overdue').fadeIn();
		return false;
	});
	$('#dashlink_overdue').click(function() {
		$('#dashboard_upcoming').hide();
		$('#dashtext_upcoming').hide();
		$('#dashlink_overdue').hide();
		$('#dashboard_overdue').fadeIn();
		$('#dashtext_overdue').fadeIn();
		$('#dashlink_upcoming').fadeIn();
		return false;
	});

	// show/hide duethisweek/duethismonth dashboard panels
	$('#dashboard_duethismonth').hide();
	$('#dashtext_duethismonth').hide();
	$('#dashlink_duethisweek').hide();
	$('#dashlink_duethismonth').click(function() {
		$('#dashboard_duethisweek').hide();
		$('#dashtext_duethisweek').hide();
		$('#dashlink_duethismonth').hide();
		$('#dashboard_duethismonth').fadeIn();
		$('#dashtext_duethismonth').fadeIn();
		$('#dashlink_duethisweek').fadeIn();
		return false;
	});
	$('#dashlink_duethisweek').click(function() {
		$('#dashboard_duethismonth').hide();
		$('#dashtext_duethismonth').hide();
		$('#dashlink_duethisweek').hide();
		$('#dashboard_duethisweek').fadeIn();
		$('#dashtext_duethisweek').fadeIn();
		$('#dashlink_duethismonth').fadeIn();
		return false;
	});

	// ajax call for deleting admin panel alerts for new messages
	$("input.delMessagesBtn").click(function(){
		$.ajax({
			url: '/wp-content/themes/websightdesigns/includes/hide_message.php',
			type: 'POST',
			data: ({id : this.getAttribute('id')}),
			timeout: 1000
		});
	});

	// ajax call for deleting admin alerts for new customers
	$("input.delCustomersBtn").click(function(){
		$.ajax({
			url: '/wp-content/themes/websightdesigns/includes/hide_customer.php',
			type: 'POST',
			data: ({id : this.getAttribute('id')}),
			timeout: 1000
		});
	});

	// ajax call for deleting admin alerts for new trouble tickets
	$("input.delTicketsBtn").click(function(){
		$.ajax({
			url: '/wp-content/themes/websightdesigns/includes/hide_ticket.php',
			type: 'POST',
			data: ({id : this.getAttribute('id')}),
			timeout: 1000
		});
	});

	// ajax call for deleting tasks
	$("a.deletetask").click(function(){
		$.ajax({
			url: '/wp-content/themes/websightdesigns/includes/delete_task.php',
			type: 'POST',
			data: ({id : this.getAttribute('id')}),
			timeout: 1000
		});
	});

	// ajax form submit for newsletter signup
	$('.newslettersubmit').click(function () {
		//Get the data from all the fields
		var email = $('.newsletterform input[name=email]');
		// form validation
		if (email.val()=='') { return false; }
		//organize the data properly
		var data = 'email=' + email.val();
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "/wp-content/themes/websightdesigns/includes/newsletter_signup.php",
			//GET method is used
			type: "GET",
			//pass the data
			data: data,
			//Do not cache the page
			cache: false,
			//success
			success: function (html) {
				//if process.php returned 1/true (send mail success)
				if (html==1) {
					//hide the form
					$('#newsletter-showform').fadeOut('fast');
					//show the success message
					$('#newsletter-showthanks').fadeIn('fast');
				//if the request returned 0 or false (send mail failed)
				} else alert('Sorry, there was an unexpected error. Please try again later.');
			}
		});
		//cancel the submit button default behaviours
		return false;
	});

	// newsletter back link
	$('#newsletter-backlink').click(function () {
		// hide the success message
		$('#newsletter-showthanks').fadeOut('fast');
		// show the form
		$('#newsletter-showform').fadeIn('fast');
		$(".newsletterform input[name=email]").val('');
	});

	// update/sync sortable tasks through ajax to mysql
	var ul = $("ul.tasks").sortable({
		axis: 'y',
		containment: '.content',
		forcePlaceholderSize: true,
		forceHelperSize: true,
		placeholder: 'ui-state-highlight',
		revert: true,
		handle: 'a.dragtask',
		update: function(event, ui) {
			$.ajax({
				url: '/wp-content/themes/websightdesigns/includes/update_sort_tasks.php',
				type: 'POST',
				data: {id : ul.sortable("serialize")},
				timeout: 1000
			});
		}
	});
	//$("ul.tasks").disableSelection();
	$("ul.tasks input:checkbox").click(function(){
		var check = $(this).attr('checked');
		var myid = this.id;
		var myurl = '';
		var myadd = '';
		var mydel = '';
		if (check) {
			myurl = '/wp-content/themes/websightdesigns/includes/update_complete.php';
			myadd = 'completed';
			mydel = 'incomplete';
		} else {
			myurl = '/wp-content/themes/websightdesigns/includes/update_incomplete.php';
			myadd = 'incomplete';
			mydel = 'completed';
		}
		$.ajax({
			url: myurl,
			type: 'POST',
			data: {id : myid},
			timeout: 1000,
			context: document.body,
			success: function(event, ui) {
				// console.log($("#title" + myid).addClass( myadd ).removeClass( mydel ), myadd, mydel)
				$("#title" + myid).addClass( myadd ).removeClass( mydel );
			}
		});
	});

	// update/sync sortable milestones through ajax to mysql
	var milestonesdiv = $("div.milestonesort").sortable({
		axis: 'y',
		containment: 'div.content',
		forcePlaceholderSize: true,
		forceHelperSize: true,
		placeholder: 'ui-state-highlight',
		revert: true,
		items: 'div.itemrow',
		handle: 'a.grabsort',
		update: function(event, ui) {
			$.ajax({
				url: '/wp-content/themes/websightdesigns/includes/update_sort_milestones.php',
				type: 'POST',
				data: {id : milestonesdiv.sortable("serialize")},
				timeout: 1000
			});
		}
	});

	// update/sync sortable projects through ajax to mysql
	var projectsdiv = $("div.projectsort").sortable({
		axis: 'y',
		containment: 'div.content',
		forcePlaceholderSize: true,
		forceHelperSize: true,
		placeholder: 'ui-state-highlight',
		revert: true,
		items: 'div.itemrow',
		handle: 'a.grabsort',
		update: function(event, ui) {
			$.ajax({
				url: '/wp-content/themes/websightdesigns/includes/update_sort_projects.php',
				type: 'POST',
				data: {id : projectsdiv.sortable("serialize")},
				timeout: 1000
			});
		}
	});
	// update/sync sortable projects through ajax to mysql, for customers view
	var projectsownerdiv = $("div.projectownersort").sortable({
		axis: 'y',
		containment: 'div.content',
		forcePlaceholderSize: true,
		forceHelperSize: true,
		placeholder: 'ui-state-highlight',
		revert: true,
		items: 'div.itemrow',
		handle: 'a.grabsort',
		update: function(event, ui) {
			$.ajax({
				url: '/wp-content/themes/websightdesigns/includes/update_ownersort_projects.php',
				type: 'POST',
				data: {id : projectsownerdiv.sortable("serialize")},
				timeout: 1000
			});
		}
	});
	//$("div.projects").disableSelection();

	// text labels
	$('input#s[title]').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));
		}

		$(this).focus(function() {
			if($(this).val() == $(this).attr('title')) {
				$(this).val('').addClass('focused');
			}
		});
		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).removeClass('focused');
			}
		});
	});
});
function none() {}
function outline() { onclick='this.blur()'; }
function HideContent(d) { document.getElementById(d).style.display = "none"; }
function ShowContent(d) { document.getElementById(d).style.display = "block"; }
function ReverseDisplay(d) {
	if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
	else { document.getElementById(d).style.display = "none"; }
}
function clearText(input){
        if (input.defaultValue==input.value)
                input.value = ''
} // usage: onfocus="clearText(this)"
function restoreText(input){
        if (input.value=='')
                input.value = input.defaultValue
} // usage: onblur="restoreText(this)"
function signoutConfirm() {
	var logout = confirm('Would you like to sign out?');
	if (logout){
		window.location = "<?php echo html_entity_decode(wp_logout_url('http://www.websightdesigns.com/mobile/')); ?>";
	}
}
