// ----------------------------------------------------------------------------
// show/hide
// ----------------------------------------------------------------------------

// show an element
function show(id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		el.style.display = "block";
	}
}
// hide an element
function hide(id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		el.style.display = "none";
	}
}

// ----------------------------------------------------------------------------
// forms
// ----------------------------------------------------------------------------

// clear default value in forms
function doClear(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = "";
	}
}
// undo clear of default value
function undoClear(theText) {
	if (theText.value == "") {
		theText.value = theText.defaultValue;
	}
}

// ----------------------------------------------------------------------------
// links and popups
// ----------------------------------------------------------------------------

// open external links in a new window ("target" is not valid XHTML strict)
function externallinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		anchor.tabindex = i;
		if (anchor.getAttribute("href")) {
			if (anchor.getAttribute("rel") == "external")
				anchor.target = "_blank";
			else if (anchor.getAttribute("rel") == "parent")
				anchor.target = "_parent";
			else if (anchor.getAttribute("rel") == "top")
				anchor.target = "_top";
			else if (anchor.getAttribute("rel") == "popup")
                $('a[rel="popup"]').click(function(){return $.dbPopWin( $(this).attr('href'));}
				);	
		}
	}
}
// dbPopWin: db Popup Windows Plugin for JQuery - 04 Jul 08
jQuery.dbPopWin = function(url, options)
{options = jQuery.extend(
		{
			/* default options */
			dbPopWinWidth:      480,
			dbPopWinHeight:     300,
			dbPopWinTarget:     'dbPopWin',
			dbPopWinScrollbars: 'yes',
			dbPopWinResizable:  'no',
			dbPopWinMenuBar:    'no',
			dbPopWinAddressBar: 'no'
		},
		options
	);
	/* center the window by default. */
	if (!options.dbPopWinY){options.dbPopWinY = screen.height / 2 - options.dbPopWinHeight / 2;};
	if (!options.dbPopWinX){options.dbPopWinX = screen.width / 2 - options.dbPopWinWidth / 2;};
	open(
		url,
		options['dbPopWinTarget'],
		'width= '      + options.dbPopWinWidth +
		',height='     + options.dbPopWinHeight + 
		',top='        + options.dbPopWinY + 
		',left='       + options.dbPopWinX + 
		',scrollbars=' + options.dbPopWinScrollbars +
		',resizable='  + options.dbPopWinResizable +
		',menubar='    + options.dbPopWinMenuBar +
		',location='   + options.dbPopWinAddressBar
	);
	return false;
};
// jumpmenu (macromedia)
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/* TextLimit - jQuery plugin for counting and limiting characters for input and textarea fields
 * pass '-1' as speed if you don't want slow char deletion effect. (don't just put 0)
 * Example: jQuery("Textarea").textlimit('span.counter',256)
 * $Version: 2007.10.24 +r1
 * Copyright (c) 2007 Yair Even-Or
 */
jQuery.fn.textlimit=function(counter_el, thelimit, speed) {
	var charDelSpeed = speed || 15;
	var toggleCharDel = speed != -1;
	var toggleTrim = true;	
	var that = this[0];
	updateCounter();	
	function updateCounter(){jQuery(counter_el).text(thelimit - that.value.length);};	
	this.keypress (function(e){ if( this.value.length >= thelimit && e.charCode != '0' ) e.preventDefault() })
	.keyup (function(e){
		updateCounter();
		if( this.value.length >= thelimit && toggleTrim ){
			if(toggleCharDel){
				// first, trim the text a bit so the char trimming won't take forever
				that.value = that.value.substr(0,thelimit+100);
				var init = setInterval
					(function(){if(that.value.length <= thelimit){ init = clearInterval(init); updateCounter()}
					else{that.value=that.value.substring(0,that.value.length-1); jQuery(counter_el).text('trimming...  '+(thelimit - that.value.length)); };
						} ,charDelSpeed 
					);}
			else this.value = that.value.substr(0,thelimit);}
	});	
};
// main function
function pageOpen() {
    $('#projects img').lazyload({effect:'fadeIn'});
	$('#projects dl').hover(
		function(){
			$('img',this).stop(true,true).fadeTo(800,0.2);
			$('.overview',this).stop(true,true).slideDown(300);},
		function(){
			$('.overview',this).slideUp(300);
			$('img',this).stop(true,true).fadeTo(800,1);}
	);
	// preselect country according to visitor's IP
    if (google.loader.ClientLocation) {
      var loc = google.loader.ClientLocation;
      $("#country").val(loc.address.country);
    };
	// enquiry form
	$('#slider').slider({min: 20,max: 1000,step: 10,range:true,slide: function(event, ui) {$('#budget').val(ui.values[0] + '.000 - ' + ui.values[1] + '.000 THB');}});
	$('#subject').change(function(){
		$('#submit_project').hide();
		$('#join_team').hide();
		$('#' + this.value).slideDown(1500);
	});
	$('#project_scope').click(function() {$('#project_time').slideDown(800);});
	$('#timeframe').change(function(){$('#project_money').slideDown(800);});
	$('.ui-slider-handle').click(function(){$('#project_description').slideDown(800);});
	$('#why_thai').click(function(){$('#thai_law').toggle();});
	// validation
	$.validator.addMethod('notEqualTo',function(value, element, params) {return this.optional(element) || value != params;});
	$('#enquiry').validate({
		onkeyup: false,
		rules: {
			name:{required:true,minlength:4},
			email:{required:true,email:true},
			country:{required:true},
			website:{url:true,required:'#scope_redesign:checked'},
			subject:{required:true},
			message:{required:true,minlength:6},
			budget:{required:'#subject[selectedIndex=1]'},
			timeframe:{required:'#subject[selectedIndex=1]'},
			team_dobday:{required:'#subject[selectedIndex=2]'},
			team_dobmonth:{required:'#subject[selectedIndex=2]'},
			team_dobyear:{required:'#subject[selectedIndex=2]'},
			team_gender:{required:'#subject[selectedIndex=2]'},
			team_references:{required:'#subject[selectedIndex=2]'},
			captcha_code:{required:true,maxlength:5}
		},
		messages: {
			name: 'Please enter your name',
			email: 'Please enter a valid email address',
			email: 'Please select your country',
			website:'Please enter a valid website address',
			subject:'Please select a subject',
			message: 'Please enter a message',
			redesign_address: 'Please enter the address of the website to redesign',
			budget: 'Please provide a budget for your project',
			timeframe: 'Please provide the timeframe of your project',
			team_dobday: 'Please enter your birth day',
			team_dobmonth: 'Please enter your birth month',
			team_dobyear: 'Please enter your birth year',
			team_gender:'Please specify your gender',
			team_references:'Please provide some references (with URL)',
			captcha_code:'Please enter the security code (click on "Reload" if you cannot read it)'
		},
		errorLabelContainer: $('#MessageContainer'),
		submitHandler: function() {
			$('#MessageContainer').hide();
			$('.button').val('Wait...');
			$('#enquiry').fadeTo('slow', 0.5);
			form.submit();
		}
		})
}

function pageClose() {
    Cufon.replace('h2');
    Cufon.replace('h3');
    Cufon.replace('h4');
    Cufon.now();
}