/**
 * common javascript functions
 *
 * @author Ethan Liu
 * @version $Id: functions.js 141 2009-09-02 13:35:36Z ethan $
 * @copyright Creativecrap.com, Ethan, 26 October, 2008
 * @package default
 **/

var l10n;

// mm scripts
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	newWin = window.open(theURL,winName,features);
	newWin.focus();
}

function popup(url, w, h, scrollbar) {
	var feature = 'toolbar=no,menubar=no,resizable=no,location=no,status=no';
	var left = Math.ceil((screen.width - w) / 2);
	var top = Math.ceil((screen.height - h) / 2);
	feature += ',scrollbars=' + scrollbar + ',width=' + w + ',height=' + h + ',top=' + top + ',left=' + left;
	setTimeout(function() {
		var popup = window.open(url, 'popup', feature);
	}, 500);
}

function focusLoginForm() {
	$("input[name='login']").focus();
}

function redirect() {
	window.location = '/user/?redirect=' + window.location.href;
}

function nl2br(str) {
	var br = '<br />';
	return (str + '').replace(/([^>]?)\n/g, '$1'+ br +'\n');
}

function addslashes(str) {
	return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\u0000/g, "\\0");
}


function validForm(form_id) {
	$(form_id + " :text,:password")
	.each(function() {
		var val = $.trim($(this).val());
		$(this).val(val);
		if (val == $(this).attr("rel")) {
			//$(this).val($(this).attr('rel')).removeClass('uniform-placeholder');
			$(this).val("").removeClass('uniform-placeholder');
		}
	});
	// check require fields
	var valid = true;
	$(form_id + " .require").each(function() {
		$(this).removeClass("uniform-form-invalid");
		//if ($(this).val() == "" || $(this).val() == $(this).attr("rel")) {
		if ($(this).val() == "") {
			$(this).addClass("uniform-form-invalid");
			valid = false;
		};
	});
	if (!valid) {
		$(form_id).uniform();
	};
	//$("#" + form_id + " .x-form-invalid:first").focus();
	//$("#" + form_id + " .x-form-invalid:first").trigger('click');
	return valid;
}

function t(str) {
	if (l10n) {
		var key = str.toLowerCase();
		str = l10n[key] ? l10n[key] : str;
	};
	return str;
}

$(function() {
	swfobject.switchOffAutoHideShow();
	$.ui.dialog.defaults.bgiframe = true;
	$("#sblogin-form").uniform({callback: function(result, status) {
		if (result.success) {
			if (result.cmd == 'redirect') {
				window.location.href = result.data;
			}
			else {
				window.location.reload();
			}
		}
	}});
	$("#sbsearch-form").uniform();
	//$("input[name='dummy_channel']").css('width', 80);
	//$("#login-box").bgiframe();
	swfobject.embedSWF("/images/pmenu.swf", "nav-lite", "990", "117", "9.0.0", {}, {"menu":false}, {"wmode":"transparent"});
	$('#dialog').dialog({ autoOpen: false });
})

function mouseenter(e) {
	$(this).addClass('hover');
}

function mouseleave(e) {
	$(this).removeClass('hover');
}

function mouseclick(e) {
	$(this).toggleClass('active');
}

function loadThumb(id, src, name) {
	var thumb = new Image();
	$(thumb).load(function() {
		var img = $("<img>").attr('src', src).attr('alt', name);
		var me = $("div.img[rel="+id+"]");
		me.wrapInner('<div class="desc"></div>').prepend(img).fadeIn('slow');
	}).attr('src', src);
	return false;
}

function loadThumbTag(f) {
	$("div.img").hide();
	$("div.img").each(function() {
		var id = $(this).attr('rel');
		if (f[id]) {
			loadThumb(id, f[id]['path'], name);
		}
	});
}

function printer() {
	setTimeout(function() {
		window.print();
	}, 800);
}

function intval(num) {
	//num = $.trim(num);
	num = parseInt(num, 10);
	if (/\D/.test(num)) {
		return 0;
	}
	return num;
}


/*
function popwin(title, w, h) {
	var id = "window";
	var win = $("#"+id);
	if (win.length > 0) {
		if (title) {
			win.find('div.title').text(title);
		};
		return win;
	};
	var left = Math.ceil((screen.width - (w+280)) / 2);
	var top = Math.ceil((screen.height - (h+160)) / 2);
	var win = $("<div></div>").addClass('window').attr('id', id).css('left', left).css('top', top);
	var header = $("<div></div>").addClass('header');
	header.append("<div class='close'>X</div>").append("<div class='title'>11</div>").append("<div style='clear:both;'></div>");
	header.find('div.title').text(title);
	header.find('div.close').live('click', function() {
		var w = popwin();
		w.hide();
	});
	var body = $("<div></div>").addClass('body');
	if (w) {
		body.css('width', w);
	};
	if (h) {
		body.css('height', h);
	};
	win.append(header).append(body);
	$("body").append(win);
	return $("#"+id);
}
*/
