// JavaScript Document
var is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );

// Preselect current page
/*$(document).ready(function(){
	$(".subnav a").each( function(){
		if ($(this).attr("href").indexOf(window.pagenow) >= 0) {
			this.className = "active";
			alert(window.pagenow);
			return;
		}
	});					   
});*/

/**
 * Short-hand function
 */
function ajax( url, params )
{
	if ( (typeof(params)).toLowerCase() == 'object' )
		params.push({name:'_ajax_request', value:'true'});
	else {
		params = [];
		params.push({name:'_ajax_request', value:'true'});
	}
	
	$.get(url, params, function(obj, status){ AjaxResponse(obj, status); });
}

function AjaxResponse( obj, status )
{
	if ( (typeof(obj)).toLowerCase() == 'object' ) {
		eval($(obj).children().text());
	} else
		alert(obj);
}

function AjaxForm( form )
{
	var url = form.attr("action");
	var a = [];
	
    $('input,textarea,select,button', form).each(function() {
        var n = this.name;
        var t = this.type;
        if ( !n || this.disabled || t == 'reset' ||
            (t == 'checkbox' || t == 'radio') && !this.checked ||
            (t == 'submit' || t == 'image' || t == 'button') && this.form.clicked != this ||
            this.tagName.toLowerCase() == 'select' && this.selectedIndex == -1)
            return;
        if (t == 'image' && this.form.clicked_x)
            return a.push(
                {name: n+'_x', value: this.form.clicked_x},
                {name: n+'_y', value: this.form.clicked_y}
            );
        if (t == 'select-multiple') {
            $('option:selected', this).each( function() {
                a.push({name: n, value: this.value});
            });
            return;
        }
        a.push({name: n, value: this.value});
    });

	ajax(url, a)
}

function doBookmark(){
	
	var appName = navigator.appName.toLowerCase(); 
	var userAgent = navigator.userAgent.toLowerCase();
	
	if (appName.indexOf("explorer") != -1){
		window.external.AddFavorite(parent.window.location, parent.document.title);
	} else {
		var info;
		if (userAgent.indexOf("mac") != -1){
			info = "Please use Cmd-D or the menu to bookmark 2440 Media";
		} else {
			info = "Please use Ctrl-D or the menu to bookmark 2440 Media";
		}
		alert(info);		
	}
	
}

function toogleValue( obj )
{
	obj = $(obj);
	
	if ( obj.val() == '' ) {
		obj.val(obj.attr('firstvalue'));	
	} else {
		// Save value
		if ( !obj.attr('firstvalue') )
			obj.attr('firstvalue', obj.val());
		
		if ( obj.val() == obj.attr('firstvalue') ) {
			obj.val('');
		}
	}
}

function validate( form, sendViaAjax )
{
	var errors = ['Ups! There are some errors:'];
	$('input,textarea', form).each(function(){
		if ( $(this).attr('alt') && $(this).attr('alt').match(/^true/i) ){
			//eval($(this).attr('alt'));
			params = ($(this).attr('alt')).split(', ');
			if ( params[0] != 'true' ) return;
			
			if ( !($(this).val()).match(eval(params[1])) ){
				errors.push(' - ' + eval(params[2]));
			} else if ( !$(this).attr('firstvalue') || $(this).attr('firstvalue') == $(this).val() ){
				errors.push(' - ' + eval(params[2]));
			}
		}
	});
	
	if ( errors.length > 1 )
	{
		alert(errors.join('\n'));
		return false;
	}
	
	if ( typeof(sendViaAjax) != 'undefined' && sendViaAjax ) {
		AjaxForm(form);	
		
		return false;
	}
	
	return true;
}

function carousel(el)
{
	var el = jQuery(el);
	var ul = jQuery('ul', el);
	var items = 4;
	var start = 0;
	var end = items; 
	// Hide prev since we at the start of the list
	jQuery('.jcarousel-prev', el).css('display', 'none');
	if (items == ul.children().length) {
		// Nothing to scroll
		jQuery('.jcarousel-next', el).css('display', 'none');
		return;
	}
	// Right to left movement
	jQuery('.jcarousel-next', el).click(function(){
		if ((end+1) <= ul.children().length) {
			ul.children().each(function(i){
				if ( i >= start && i < end)
					jQuery(this).fadeOut("fast");
			});
			start = end;
			end += items;
		} 
		// Hide next button
		if (end >= ul.children().length)
			jQuery(this).css('display', 'none');
		// Show prev if needed
		if (jQuery('.jcarousel-prev', el).css('display') == 'none')
			jQuery('.jcarousel-prev', el).css('display', '');	
	});
	// Left to right movement
	jQuery('.jcarousel-prev', el).click(function(){
		if (start - items >= 0) {
			end = start; 
			start -= items;
			ul.children().each(function(i){
				if ( i >= start && i < end)
					jQuery(this).fadeIn("fast");
			});
		} 
		// Hide prev button
		if (start == 0)
			jQuery(this).css('display', 'none');
		// Show prev if needed
		if (jQuery('.jcarousel-next', el).css('display') == 'none')
			jQuery('.jcarousel-next', el).css('display', '');	

	});	
}

function setProject(company, desc, url, src)
{
	$("h2,.work,#workSelected img", jQuery('#submiddleWindow')).fadeOut("fast", function(){
		$("h2", jQuery('#submiddleWindow')).text(company);
		$(".work p", jQuery('#submiddleWindow')).html(desc);
		$("#workSelected a, .work .viewproject", jQuery('#submiddleWindow')).attr("href", url);
		$("#workSelected img", jQuery('#submiddleWindow')).attr("src", src);
	});	
}
