/**
 * The default theme javascript file
 *
 * Should be loaded last of all the JS files
 */
 
var vform = function(){
	$('#fancybox-content form').validate({
		rules: {
						telephone:{
							required: true,
							notSameAsDefault:true
						},
						name:{
							required: true,
							notSameAsDefault:true
						},
						company:{
							required: true,
							notSameAsDefault:true
						},
						email:{
							required: true,
							email: true,
							notSameAsDefault:true
						}
					},
		messages: {
				  name: "This Field is Required",
				  telephone: "This Field is Required",
				  company: "This Field is Required",
				  email: "This Field is Required"
		},
		wrapper: 'div class="error" ',
		errorPlacement: function(error, element){error.insertAfter(element).css({'opacity':'0','display':'block'}).animate({'opacity':1},500);
		}
	});
}
 
var asm = function(){
	$('#fancybox-content #nature').selectmenu({
		width: 280,
		open: function(){$(this).selectmenu('disable',0)}
	});
	
	$('#fancybox-content #referrer').selectmenu({
		width: 280,
		open: function(){$(this).selectmenu('disable',0)},
		select: function(event,options){if($(this).val()=='Other (Please specify)'){$('input.other').css('display','block');}else{$('input.other').css('display','none').val('');}}
	});
};

$(document).ready(function() {

	
	if($('#latest-news-widget-3 h1.widget-title').length>0){
		$('#latest-news-widget-3 h1.widget-title').html('<a href="/news" title="Read more news">News</a>').find('a').css({'border-bottom':'none','text-decoration':'none'});
	}
	
	jQuery.validator.addMethod("notSameAsDefault", function(value, element) {
		return this.optional(element) || (value != $(element).attr('title'));
	}, "This field is required");
	// activate columnizr
	if(!$('html').hasClass('csscolumns')){
		$('#footer ul.multi-col .column li').addClass('dontsplit');
		$('#footer ul.multi-col').columnize({columns:3});
		$('#footer ul.multi-col li').addClass('dont-split').css('display','inline-block');
		$('#footer ul.multi-col ul').css({'display':'inline-block'});
		$('#footer ul.multi-col .column').css('margin-right','15px');
		$('#footer ul.multi-col .last.column').css('margin-right','0');
	}
	$('header nav li').eq($('header nav li').length-2).addClass('last');
	
	$("a.cata-order").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false,
		'onClosed'		: function() {
		},
		'onStart'		: function() {
			$('body').removeClass('thankyou');
			setTimeout("asm()",300);
		},
		autoScale: false,
		width: 960,
		height: 760,
		overlayOpacity: 1,
		autoDimensions: false,
		padding:0
	});
	//alert($('a.cata-order').attr('id'));
	if(window.location.hash == '#order-form'){
		$('body').addClass('thankyou');
	  _gaq.push(['_setAccount', 'UA-880412-1']);
	  _gaq.push(['_trackPageview','/thank_you.html']);
		var strg = $('#cata-thanks').html()
		$.fancybox(strg,{
			'scrolling'		: 'no',
			'titleShow'		: false,
			'onClosed'		: function() {
			},
			'onStart'		: function() {
				$('#fancybox-close').click(function(event){
					event.preventDefault();
					history.go(-2);
				});
			},
			autoScale: false,
			width: 960,
			height: 760,
			overlayOpacity: 1,
			autoDimensions: false,
			padding:0,
			hideOnOverlayClick: false
		});
	}else if(window.location.hash == '#open-form' || $('a.cata-order').attr('id') == '#auto-launch'){
		var strh = '';
		$.fancybox('<div id="order-form-div">'+$('#order-form-div').html()+'</div>',{
			'scrolling'		: 'no',
			'titleShow'		: false,
			'onClosed'		: function() {
			},
			'onStart'		: function() {
				$('body').removeClass('thankyou');
				//alert($('#order-form').length);
				setTimeout("vform()",300);
				$('#fancybox-close').click(function(event){
					event.preventDefault();
					history.go(-1);
				});
			},
			autoScale: false,
			width: 960,
			height: 760,
			overlayOpacity: 1,
			autoDimensions: false,
			padding:0,
			hideOnOverlayClick: false
		});
		
	}
	
	$('#order-form input, #order-form textarea').toggleVal({populateFrom: 'title'});
	
	$('#order-form').validate({
		rules: {
						telephone:{
							required: true,
		notSameAsDefault:true
						},
						name:{
							required: true,
		notSameAsDefault:true
						},
						company:{
							required: true,
		notSameAsDefault:true
						},
						email:{
							required: true,
							email: true,
		notSameAsDefault:true
						}
					},
		messages: {
				  name: "This Field is Required",
				  telephone: "This Field is Required",
				  company: "This Field is Required",
				  email: "This Field is Required"
		},
		wrapper: 'div class="error" ',
		errorPlacement: function(error, element){error.insertAfter(element).css({'opacity':'0','display':'block'}).animate({'opacity':1},500);
		}
	});
	//
	// Reset search form value on focus/blur 
	//
	var $searchField = $('#search-form input.textfield'),
		searchFieldValue = $searchField.val();
    $searchField.focus(function() {
    	if($(this).val() === searchFieldValue) {
        	$(this).val('');
		}
	}).blur(function() {
      	if(!$(this).val()) {
           	$(this).val(searchFieldValue); 
		}
	});
	
	$('#fancybox-content #nature').selectmenu({
		width: 280,
		open: function(){$(this).selectmenu('disable',0)}
	});
	
	$('#fancybox-content #referrer').selectmenu({
		width: 280,
		open: function(){$(this).selectmenu('disable',0)},
		select: function(event,options){if($(this).val()=='Other (Please specify)'){$('input.other').css('display','block');}else{$('input.other').css('display','none').val('');}}
	});
	$('#latest-news-widget-3').find("div.post:gt(1)").remove();
});

