$(document).ready(function() {
//	$('input[@type=text]').focus(function(){
//		var currentVal = $(this).val();										// Read initial field value
//		$(this).val('');														// Clear initial value
//
//		$(this).blur(function(){
//			var getNewVal = $(this).val();										// Read new input value
//			if ( getNewVal == '' || getNewVal == ' ' ) {
//				$(this).val(currentVal);										// Switch to initial value
//			}
//		});
//	});

	// fix png for IE
	$('img[@src$=.png], div').ifixpng();

	// Safari CSS
	if ( $.browser.safari ) {
		$("head").append('<link href="css/safari.css" media="screen" rel="stylesheet" type="text/css" />');
	}

	// ul.images fix
	if ( $('ul.images li').length ) {
		var liIndex = $('ul.images li').length;
		for ( var i = 0; i <= liIndex; i++ ) {
			if ( i%3 == 0 ) {
				$('ul.images li:nth-child('+i+')').css({marginRight:'0'});
			}
		}
	}
	
	// foreground color for H4 based on current left section
	var readColor = $('#left-side ul li a.on').css('color');
	$('#right-side ul.designer li h4, #right-side ul.articles li h4').css({color:readColor,borderBottom:'1px solid '+readColor});
});

// Define indexOf for IE
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};

function validate()
{
	var msg = "";
	var err = 0;
	var email_pattern = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
	$(".required").each(function(){
		if ($.trim($(this).val()).length < 3)
		{
			$(this).css("border","1px solid red");
			msg += "Please complete "+$(this).attr('title')+".\n";
			err++;
		} else if ($(this).attr('title')=="Email" && $(this).attr("class","required")) {
			if (!$(this).val().match(email_pattern))
			{
				$(this).css("border","1px solid red");
				msg += "Please complete "+$(this).attr('title') +" with a valid email address.\n";
				err++;
				
			} else {
				$(this).css("border-color","#666");
			}
		} else {
			$(this).css("border-color","#666");
		}
	});
	if (err > 0)
	{
		alert("There are some errors:\n"+msg);
		return false;
	} else {
		return true;
	}
}
$(function(){
	$(".designer_thumb").click(function(){
		$("#preview_image").attr('src',$(this).attr('src').replace("_small","_medium"));
		$("#preview_imagelink").attr('href',$(this).attr('src').replace("_small",""));
	});
	$(".video_preview").popup({
		width: 425,
		height:330
	});
	$("a.choose_video").click(function(){
		var id = $(this).attr('title');
		var params = {'id':id};
		$.getJSON("/ajax.php",params,function(data){
			$('h3.video_title').html(data.title);
			$('p.video_content').html(data.content);
			eval(data.script);
		});
//		$.getScript("/ajax.php?id="+id);
	});
});

/* POPUP Plugin */
/*
* jQuery POPUP Plugin v1.0
*
* easy window.open popups
* @requires metadata plugin
*/
(function($){
	// plugin definition
	$.fn.popup = function (options){
		var options = $.extend({}, $.fn.popup.defaults, options);
		return this.each(function (){
			$(this).click(function (){
				$this = $(this);
//				var features = $this.metadata() ? $.extend({}, options, $this.metadata()) : options;
				var features = $this.metadata && $this.metadata() ? $.extend({}, options, $this.metadata()) : options;
				if (features['width'] == 'auto'){features['width'] = Math.floor(screen.width/2);}
				if (features['height'] == 'auto'){features['height'] = Math.floor(screen.height/2);}
				if (features['left'] == 'auto'){features['left'] = Math.floor((screen.width-features['width'])/2);}
				if (features['top'] == 'auto'){features['top'] = Math.floor((screen.height-features['height'])/2);}
				
				var url = this.href;
				var target = '_blank';
				var s_features = [];
				for (feature in features){
					if (feature == 'target'){target = features[feature];}
					if (feature == 'url'){url = features[feature];}
					else{s_features[s_features.length] = feature+'='+features[feature];}
				}
				var win = window.open(url, target, s_features.join(','));
				if (win){win.focus();return false;}
				return true;
			});
		});
	}
	// plugin defaults
	$.fn.popup.defaults = {
		status:			1,
		toolbar:		0,
		resizable:		1,
		scrollbars:		1,
		width:			'auto',
		height:			'auto',
		top:			'auto',
		left:			'auto'
	};
})(jQuery);
