$(document).ready(function(){

	/* Highlight fields when selected */
	$(".field, .bigfield").focus(function(){ $(this).addClass("selected"); });
	$(".field, .bigfield").blur(function(){ $(this).removeClass("selected"); });
	
	/* Select a field on the page automatically */
	$(".autofocus").focus();
	
	/* Select all text upon enterting field */
	$('.autoselect').live('click',function(){
		$(this).select();
	});
		
	/* Button IE fix */
	$("a.button, a.bigbutton, a.biggreen").live('click',function(event){
		this.blur();
	});

	/* Warn when features are todos	 */
	$("a").live('click',function(event){
		loc = $(this).attr('href');
		if(loc == '#todo'){
			alert('This feature is still under development.');
			return false;
		}
	});
	
	/* Makre required field asterisks red */
	$('.form_div label, .note').each(function(){
		a = $(this).html();
		a = a.replace('*','<span class="clr8 bld">*</span>');
		$(this).html(a);
	});
	
	/* Submission buttons */
	$(".submission a.button:not(.clear), .submission a.bigbutton:not(.bigclear)").live('click',function(event){
		//$(this).parent('.submission').html('<p class="working_msg">Working...</p>');
	});
	
	/* $et styles for qtips */
	$.fn.qtip.styles.resumator = {
		padding: 15,
		color: '#39381E',
		background: '#f5fbff',
		width: {
			min: 250
		},
		border: {
			width: 1,
			radius: 5,
			color: '#C8E9FF'
		},
		tip: 'topLeft',
		name: 'light'
	}
	
	$.fn.qtip.styles.modal = { 
		padding: 0,
		color: '#39381E',
		background: '#FFF',
		width: { 
			min: 400,
			max: 900 
		},
		border: {
			width: 4,
			radius: 4,
			color: '#39381E'
		},
		name: 'light'
	}
	
	/* Make default adjustments to qtip */
	$.fn.qtip.defaults.position.corner.target = "bottomcenter";
	$.fn.qtip.defaults.position.adjust = {
		x: 0,
		y: -10,
		'screen': true
	}
	
	/* Get the button widgets */
	$('#btn_get_widgets').qtip({ 
		content: $('#widgetbox'),
		position: { 
			target: $(document.body),
			corner: {
				target: 'topMiddle',
				tooltip: 'topMiddle'
			},
			adjust: {
				y: 75
			}
		},
		show: { when: 'click', solo: true },
		hide: { when: 'unfocus' },
		style: 'modal',
		api: { beforeShow: function(){ $('#dimmer').show(); }, beforeHide: function(){ $('#dimmer').hide(); }
		}
	});
	$('#btn_get_widgets').click(function(){
		return false;
	});
	
	/* Close the widgetbox */
	$('#close_widgetbox').click(function(){
		$('#btn_get_widgets').qtip('hide');
		return false;
	});
	
	set_dimmer();
	
});

function set_dimmer(){
	if( $('#dimmer').is(':visible') ) {
		dimmer_visible = true;
	}else{
		dimmer_visible = false;
	}
	$('#dimmer').remove();
	$('<div id="dimmer">')
		.css({
			position: 'absolute',
			top: 0,
			left: 0,
			height: $(document).height(),
			width: '100%',
			opacity: 0.9,
			backgroundColor: '#CCDCE6',
			zIndex: 5000
			})
		.appendTo(document.body)
		.addClass('none');
	if(dimmer_visible){
		$('#dimmer').fadeIn('fast');
	}
}

function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
	histogram['%2F'] = '+';
	histogram['%26'] = '';
	histogram['%25'] = '';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function setCookie(c_name,value,expiredays,path){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
	";path="+path;
	return false;
}

function check_submission_form(){
	resumator_errors = false;
	resumator_firstname = $("#resumator-firstname-value");
	resumator_lastname = $("#resumator-lastname-value");
	resumator_email = $("#resumator-email-value");
	usr_password1 = $("#resumator-passcode-value");
	if( resumator_firstname.attr("value") == "" ){
		alert("Error: Enter your first name.");
		resumator_firstname.focus();
		return false;
	}
	if( resumator_lastname.attr("value") == "" ){
		alert("Error: Enter your last name.");
		resumator_lastname.focus();
		return false;
	}
	if( !check_email( resumator_email.attr("value") ) ){
		alert("Error: Enter valid email address.");
		resumator_email.focus();
		return false;
	}
	if( usr_password1.attr("value") == "" || usr_password1.attr("value").length < 7 || usr_password1.attr("value").length > 30 ){
		alert("Error: Enter a password between 7 and 30 characters.");
		usr_password1.focus();
		return false;
	}
	return true;
}

function check_email(email_address){
	if(email_address.search(/^[a-zA-Z0-9_\+]+((-[a-zA-Z0-9_\+]+)|(\.[a-zA-Z0-9_\+]+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) {
		return false;
	}else{
		return true;
	}
}

function urlparams(params){
	result = '';
	for(var i = 0; i < params.length; i++){
		result += '/'+urlencode($(params[i]).attr('value'));
	}
	return result;
}
