$(document).ready(function() {
		
	Cufon.replace($('.controller-image h1, .swiss, .controller-image span'), 'Swiss');
	Cufon.replace($('.swiss-light'), 'swiss-light'); /* Only for testing /main/test/ */
	
	if ( $('#contact_confirm').length > 0 ) { setTimeout("fadeContactConfirm()",30000); }	
	
	Shadowbox.init({
		overlayOpacity:0.75,
	   onFinish: function() {
	   		$('#sb-nav-close').appendTo('#sb-title');
			$('#sb-title').css({'background':'url('+ path +'/images/sitewide/westech-video-player-background.jpg) top left repeat-x'});
	   		$('#sb-title').append('<img class="video-logo" src="'+path+'/images/sitewide/westech-video-player.jpg" alt="Westech Video Player" />');
	   },
	   onClose: function() {
			$('.video-logo').remove();
			$('#sb-nav-close').appendTo('#sb-nav');
			$('#sb-title').css({'background':'none'});
	   }
	});	
	
	form_replace();
	first_last();
	
	$('nav#products ul li a').corner("top 4px");
	$('#events_table tbody tr:last td').css({'border':'0px'});
	
	$('#lang-select .top').hover(
		function() { $(this).find('ul:first').css({'display':'block'}); },
		function() { $(this).find('ul:first').css({'display':'none'});  }
	);
	
	$('#lang-select .top ul:first li a').click(function() {
		$('#language').attr('value',$(this).html());
	});
	
	if ( $('#contact-us-flash').length > 0 ) {
		var flashvars = {xmlLoc: URI_CONTROLLER + '/xml/', color:'0xbce2f8' };
		var params = {wmode:"transparent"};
		swfobject.embedSWF(path + "/flash/westech_contacts.swf", "contact-us-flash", "967", "460", "9.0.0", path + "/flash/expressInstall.swf", flashvars, params);
	}

	if ( SHOW_INTRO == true ) {
		var select = $('header > form, header > nav, #index-lower-content > .container, footer, .index-container');
		var i = 0;
		
		var file = path + '/videos/westech_intro.mp4';
		var width = 843;
		var height = 480;
	
		if ( $(window).width() > 1280 && $(window).height() > 810 ) { // Change if we can support the larger area (810 includes the header)
			file = path + '/videos/westech_intro_large.mp4';
			var width = 1280;
			var height = 720;
			$('#index-flash-container').animate({height:'720px'});
		}
		
		$(select).fadeTo(50, 0, function() {
			$('#index-lower-content').css({'height':'200px'});
			$('#head').css({'height':'70px'});
			$('.index-container').css({'height':'358px'});
			$(select).css({'display':'none'});
			
			if ( i == 4 ) {
				$('#index-flash-container').html('<div id="video_gallery_player"></div>').delay(100).animate({height:height}, 250, function() {
					$('#video_gallery_player').css({'height': height, 'width': width});
					
					var flashvars = {v:file, f:'intro_reset'};
					swfobject.embedSWF(path + '/flash/westech_vid.swf', "video_gallery_player", width, height, "10.0.0", path + "/public/flash/expressInstall.swf", flashvars);
				});
				
				// Attach a skip button
				$('header').append('<a class="button skip" href="#">Skip Intro<span></span></a>');
				
				$('.skip').css({'position':'absolute', 'left':'50%', 'margin':'30px 0 0 450px'}).click(function() {
					intro_reset();
				});
		
			}
			i++;
		});

	} else {
		if ( $('#index-flash-container').length > 0 ) {
			var flashvars = {xmlLoc: path + '/xml/westech_splash.xml' };
			var params = {wmode:"transparent"};
			swfobject.embedSWF(path + "/flash/westech_splash.swf", "index-replace", "1200", "357", "9.0.0", path + "/flash/expressInstall.swf", flashvars, params);
		}
	}	
	
	if ( $('#form_contact').length > 0 ) {
		$('#referer').comboBox({valTrigger: 'other'});
	}
	
	$('#login-form #submit').click(function() {
		$('#login-form').submit();
		return false;
	})
	

	if ( $('#videoPlaceHolder').length > 0 ) {
		$('#videoPlaceHolder').each(function() {
			
			// Lets make a little jwplayer object...
			id = $(this).attr('id');

			var script = '<embed flashvars="file='+ $(this).attr('data-path') +'&autostart=true" allowfullscreen="true" allowscripaccess="always" id="'+$(this).attr('id')+'" name="player1" src="'+URI_SKIN+'/public/swf/player.swf" width="'+$(this).css('width')+'" height="'+$(this).css('height')+'" style="'+$(this).attr('style')+'" />';
			$(this).after(script);
			$(this).remove();
			
			$('#'+id).css({'display':'block'});
			
			
			// Remove place holder from the dom. 
	//		$(this).remove();
			
		//	console.log($(obj).attr('style'));
			
	//		var parts = $(obj).css().split(";")
			// for (var i=0;i<parts.length;i++) {
			// 	
			// 	console.log(parts[i]);
			// 	
			// 	// var subParts = parts[i].split(':');
			// 	// obj[subParts[0]]=subParts[1];
			// 
			// 
			// }
			
			
			
			
		});
	}
	
});


$.fn.comboBox = function(opts) {
	var conf = {
		valTrigger: -1
	};
	if (opts) { $.extend(conf, opts); }

	return this.each(function() {

		var selectElem = $(this);
		
		
		if ($(this).val() == conf.valTrigger) {
			selectElem.next('input[type="text"]').show();
		} else {
			selectElem.next('input[type="text"]').hide();
		}
		$(this).change(function(e) {
			if ($(this).val() == conf.valTrigger) {
				selectElem.next('input[type="text"]').show();
			} else {
				selectElem.next('input[type="text"]').hide();
			}
		});
	});
};



function fadeContactConfirm() {
	$('#contact_confirm').fadeOut();
}


function intro_reset() {
	// This function detaches the flash video and re-sets the design when called.
	var select = $('header > form, header > nav, #index-lower-content > .container, footer');
	$('.skip').remove();
	
	$('#index-flash-container').html('<div id="index-flash-container"><div id="index-replace"></div></div>').delay(100).animate({height:'358px'}, 350, function() {
		var flashvars = {xmlLoc: path + '/xml/westech_splash.xml' };
		var params = {wmode:"transparent"};
		swfobject.embedSWF(path + "/flash/westech_splash.swf", "index-replace", "1200", "357", "9.0.0", path + "/flash/expressInstall.swf", flashvars, params);
	});
	$('#index-flash-container').queue(function() {
		$(select).delay(300).fadeTo(1, 300, function() {
			$('#index-lower-content').animate({'height':'275px'}, 200);
		});

		$(this).dequeue();
	})
	
}

function first_last() {
	$('ul, ol, dl, tr').each(function() {
		$(this).find('li:first, dd:first, dt:first, td:first').addClass('first');
		$(this).find('li:last, dd:last, dt:last, td:last').addClass('last');
	});
}

function rmPix(varin) {
	out = varin.replace("px", "") * 1;
	return out;
}

function form_replace() {
	$('form').find('input:text, textarea').each(function() {
		if ( $('body').attr('class') == 'admin' ) {} else {
			$(this).focus(function() { 
				if ($(this).attr('rel') != undefined && ($(this).val().toLowerCase() == $(this).attr('rel').toLowerCase())) {
					$(this).val(''); 
				}
			});
			$(this).blur(function() {
				if ( $.trim($(this).val()) == '' ) { 
					$(this).val($(this).attr('rel')); 
				}
			});
		}
	});
}

function even_divs(obj) {
	big = 0;
	$(obj).each(function() {
		var height = $(this).height();
		if ( height >= big ) { big = height; } // Get the largest object
	});
	$(obj).each(function() { 
		current = rmPix($(this).css('padding-bottom')); //Current padding
		height = $(this).height();
		var padding = (big - height) + current;//new padding based on need to expand. 
	//	$(this).delay(125).animate({paddingBottom: padding}, 250).delay(125);
		$(this).delay(50).css({'padding-bottom':padding});
	});
}
