/*
var swfversion = deconcept.SWFObjectUtil.getPlayerVersion();
var noflash = deconcept.util.getRequestParameter("noflash");
var printerfriendly = deconcept.util.getRequestParameter("printerfriendly");
var flash = deconcept.util.getRequestParameter("flash");
if (flash=='yes') eraseCookie('flash');
if (flash=='no'||readCookie('flash')=='no') {createCookie('flash','no',1); noflash=1;}
if (document.getElementById && (swfversion['major'] > 8) && noflash=='' && printerfriendly=='') {
	document.writeln("<STYLE>.divFlash{display:block;}.divHtml{display:none;}</"+"STYLE>");
}
*/

var debug = true;
/* ==================== */

$(document).ready(function(){

	$('#fade').cycle({
		fx:     'fade',
		timeout: 10000,
		delay:  -2000,
		pause: 1,
   	 	pauseOnPagerHover: 1,
		containerResize: 1
	});
	$("#order").validate({
		rules: {
			firstname: "required",
			lastname: "required",
			wphone: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: {
				email: "Not a valid email"
			}
		}
	});
	
	init();
})

/* this function is called when the page is ready or after an ajax call has completed */

function init() {

	/* --- Read More toggle --- */
	$('.readmorecontent').hide();
	var c = 1;
	$('.readmorelink').each(function(){$(this).attr('rel',c++)});
	c = 1;
	$('.readmorecontent').each(function(){$(this).attr('rel',c++)});
	$('.readmorelink').toggle(function() {
		$(this).addClass('less').html('(READ LESS)...');
		$(".readmorecontent[rel='"+$(this).attr('rel')+"']").slideDown('slow',function(){$(this).show()});
	}, function() {
		$(this).removeClass('less').html('(READ MORE)...');
		$(".readmorecontent[rel='"+$(this).attr('rel')+"']").slideUp('slow',function(){});
	});


	$(".iframe").fancybox({
		'hideOnContentClick': false,
		'showCloseButton': true,
		'width': 384,
		'height': 220
	});
	$(".contactMedia").fancybox({
		'hideOnContentClick': false,
		'showCloseButton': true,
		'width': 450,
		'height': 350,
		'type': 'iframe'
	});
	$(".contactInvestor").fancybox({
		'hideOnContentClick': false,
		'showCloseButton': true,
		'width': 450,
		'height': 350,
		'type': 'iframe'
	});

	$(".video1").fancybox({
		'hideOnContentClick': false,
		'showCloseButton': true,
		'width': 640,
		'height': 360,
		'type': 'iframe',
		'scrolling': 'no'
	});

	/* Add a fancy box to all items on widget_rss that contain a link to 'irconnect' */
	$(".widget_rss .item .header .title a[href*=irconnect], .news.press .newstitle a").fancybox({
		'hideOnContentClick': false,
		'showCloseButton': true,
		'width': 680,
		'height': 480,
		'type': 'iframe',
		'scrolling': 'auto'
	});

}

function clearText(thefield){
	if (thefield.defaultValue==thefield.value) thefield.value = ""
}

function postionHeader() {
	var body_top = parseInt($('body').css('padding-top').replace(/px/,''));
	var header_top = parseInt($('#header').css('padding-top').replace(/px/,''));
	
	if (isTall) {
		$('#header').height($('#header .row3').height()-header_top);
		
		$('#header .row3').css('position','absolute');
		$('#header .row3').css('top',(body_top)+'px');
		$('#header .row3').width($('#header').width());
	}
	
	if ($('#header .row4').height()!=null) {
		$('#header .row4').css('position','absolute');
		$('#header .row4').width($('#header').width());
		if (isTall) {
			$('#header .row4').css('top',(body_top+$('#header .row3').height()-$('#header .row4').height())+'px');
		} else {
			$('#header .row4').css('top',(body_top+header_top+$('#header .row1').height()+$('#header .row2').height()+$('#header .row3').height()-$('#header .row4').height())+'px');
		}
	}
}

/* ==================== */

function createCookie(name,value,days) {
	var expires="", date=new Date();
	if (days) {
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires="; expires="+date.toGMTString();
	}
	document.cookie=name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ=name+"=", ca=document.cookie.split(';'), c="";
	for(var i=0;i < ca.length;i++) {
		c = ca[i];
		while (c.charAt(0)==' ') c=c.substring(1,c.length);
		if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {createCookie(name,"",-1);}

/* ---------------------------------- */
/* ----- Dialogs ----- */

var dialogCurrentName = "";

function dialogOpen(name,url,data,showOverlay) {
	if (name=="") name = dialogCurrentName;
	if (typeof(data)=="undefined") data = '';
	if (typeof(showOverlay)=="undefined") showOverlay = true;

	// Use redirect for virtual pages
//	if (url.indexOf('.htm')>0)		url = "/redirect.cfm?url=" + url;
	if (url.indexOf('?')<0)		url += "?";
	url += data;
	if (url.indexOf('&name=')<0)	url += "&name="+escape(name);

	if ($('#dialog_'+name).length == 0) {

		var dialogHtml = "";
		dialogHtml += "<div id='dialog_"+name+"' class='dialog'>";
		dialogHtml += " <div class='dialog_t'>";
		dialogHtml += "	 <div class='dialog_tl'></div>";
		dialogHtml += "	 <div class='dialog_tr'></div>";
		dialogHtml += "	 <div class='dialog_tm'></div>";
		dialogHtml += " </div>";
		dialogHtml += " <div class='dialog_l'>";
		dialogHtml += "  <div class='dialog_r'>";
		dialogHtml += "   <div class='dialog_m'>";
		dialogHtml += "    <div class='dialog_close'><a class='but but_off' href='#' onClick=\"dialogClose('"+name+"');return false\">Close</a></div>";
		dialogHtml += "    <div class='dialog_content'>";
		dialogHtml += "     <iframe src='"+url+"' frameborder=0 scrolling='no' name='"+name+"'></iframe>";
		dialogHtml += "    </div>";
		dialogHtml += "    <div class='clearall'></div>";
		dialogHtml += "   </div>";
		dialogHtml += "  </div>";
		dialogHtml += "	</div>";
		dialogHtml += "	<div class='dialog_b'>";
		dialogHtml += "	 <div class='dialog_bl'></div>";
		dialogHtml += "	 <div class='dialog_br'></div>";
		dialogHtml += "	 <div class='dialog_bm'></div>";
		dialogHtml += "	</div>";
		dialogHtml += "</div>";

//		dialogHtml += "<div id='dialog_"+name+"' class='dialog_box'></div>";
		$('body').append(dialogHtml)
		
	} else {

		var h = $('#dialog_'+name+' .dialog_content').height();
		$('#dialog_'+name+' .dialog_content').html("<div class='loading' style='height:"+h+"px'></div>");
		
	}

//	$('#dialog_'+name+' .dialog_close').css("visibility","hidden");


/*
	$.ajax({
		type: 'post',
		url: url,
		data: data,
		dataType: 'html',
		success: function(data, textStatus) {
			dialogOpen_complete(name,data);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			//var a = "";	for(i in XMLHttpRequest) a+=i+"=["+XMLHttpRequest[i]+"]<br>";
			dialogOpen_complete(name,"<div class='box'>"+textStatus+"</div>");
		}
	});
*/

	if (dialogCurrentName=='' && showOverlay) {
		$('#dialog_overlay div').addClass('on');
		$('#dialog_overlay').fadeIn('slow');
	}

	return false;
}

function dialogOpen_complete(name,dialogWidth,dialogHeight) {
	trackEvent('Open Dialog', name);

/*
	$('#dialog_'+name+' .dialog_close').css("visibility","visible");
	$('#dialog_'+name+' .dialog_content').html(data);
*/
	//$('#dialog_'+name+' .dialog_content iframe').html(data);


//	var dialogWidth = $('#dialog_'+name+' .dialog_content iframe #contents').width();
//	var dialogHeight = $('#dialog_'+name+' .dialog_content iframe #contents').height();
//alert("'#dialog_"+name+"'\n"+dialogWidth+"\n"+dialogHeight);

$('#dialog_'+name+' .dialog_content iframe').css('width',dialogWidth+'px');
$('#dialog_'+name+' .dialog_content iframe').css('height',dialogHeight+'px');

$('#dialog_'+name).css('width',(dialogWidth+10)+'px');
$('#dialog_'+name).css('height',(dialogHeight+80)+'px');
$('#dialog_'+name).css('display','none');

	var pit = positionIt();

	var top = pit.ScrollTop + parseInt((pit.ViewHeight/2)-(dialogHeight/2));
	if (top < pit.ScrollTop) top = pit.ScrollTop+5;

	var left = pit.ScrollLeft + parseInt((pit.ViewWidth/2)-(dialogWidth/2));
	if (left < pit.ScrollLeft) left = pit.ScrollLeft+5;

	$('#dialog_overlay div').removeClass('on');

//alert(left+"\n"+top);
/*
	if (dialogCurrentName != '') {
//		curtop = $('#dialog_'+name).css('top');
//		$('#dialog_'+name).css('top',top);
		$('#dialog_'+name).animate({ 
			top: top,
			left: left
		}, 500 );
	} else {
*/
		$('#dialog_'+name).css('left',left);
		$('#dialog_'+name).css('top',top);
		$('#dialog_'+name).fadeIn('fast');
/*
	}
*/

	dialogCurrentName = name;

	if (debug) {
		$('#eh_debug').html('pit.ScrollTop['+pit.ScrollTop+'] dialogHeight['+dialogHeight+'] pit.ViewHeight['+pit.ViewHeight+'] top['+top+']');
	}

}

function dialogClose(name,hideOverlay) {
	if (typeof(hideOverlay)=="undefined") hideOverlay = true;
	if (name=='') name = dialogCurrentName;
	
	dialogCurrentName = "";

	$('#dialog_'+name).fadeOut('fast',function(){
		//$('#dialog_'+name+' .content').html('');
		$('#dialog_'+name).remove();
	});

	if (hideOverlay) {
		$('#dialog_overlay').fadeOut('slow');
	}
	
	return false;
}

/* ---------------------------------- */

function submitForm(objForm) {
	//alert(form.action);

	dialogOpen('', objForm.action, $(objForm).serialize());

	return false;
}


/*
function serializeForm(form) {
	var data = "";
	$("#"+form+" :input").each(function (i) {
		v = $(this).val();
		t = $(this).attr('type');
		if (t=='checkbox') if ($(this).attr('checked')!=true) v = "";
		data += $(this).attr('name')+"="+escape(v)+"&";
	});
	return data.substr(0,data.length-1);
}
*/

function parseAjaxComplete(XMLHttpRequest) {
	var res = new Object();
	var a = XMLHttpRequest.split('&');
	for(var i in a) {
		var d = a[i].split('=');
		res[d[0]] = '';
		if (d.length>0) res[d[0]] = unescape(d[1]);
	}
	return res;	
}

function positionIt() {
	
	var ScrollTop = document.body.scrollTop;
	var ScrollLeft = document.body.scrollLeft;
	if (ScrollTop == 0) {
		if (window.pageYOffset) {
			ScrollTop = window.pageYOffset;
			ScrollLeft = window.pageXOffset;
		} else {
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
			ScrollLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
		}
	}

	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}

	var viewportwidth = winW;
	var viewportheight = winH;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	}
	// older versions of IE
	else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	return {ScrollTop:ScrollTop,ScrollLeft:ScrollLeft,WindowWidth:winW,WindowHeight:winH,ViewWidth:viewportwidth,ViewHeight:viewportheight};
}

function trackEvent(action,labels) {
	if (typeof(pageTracker)!="undefined") {
		//pageTracker._trackEvent('EH2010', action, labels);
	}
}

function NavigateAfterMoreThanLength(obj,length){
	if (obj.value.length>=length && !(event.keyCode == 8 || event.keyCode== 46)) {
		alert('NextControl.focus()');
		return;
	}
}
