window.onerror=function(msg, url, linenumber){
 try{console.log('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber)}catch(e){}
 return true
}

function mkPopup(loc,X,Y,name){
	popup = window.open(loc,name,'width='+X+',height='+Y+',menubar=0,scrollbars=1,toolbar=0,location=1,directories=0,resizable=1,status=1');
	try{
		var aW = (screen.availWidth - X - 4) / 2;
		var aH = (screen.availHeight - Y - 4) / 2;
		popup.moveTo(aW,aH);
	}catch(e){}
	if (window.focus)
		popup.focus();
	return false;
}

function createAjax(){
	e = document.createElement('div');
	document.body.appendChild(e);
	e.id = 'ajax';
	try{
		p = document.createElement('p');
		p.style.color = 'rgba(1,1,1,0.5)';
		if(/^rgba/.test(p.style.color)){
			e.style.border = '10px solid rgba(51,51,51,0.6)';
			nUa = navigator.userAgent.toLowerCase()
			if (/webkit/i.test(nUa)){
				e.style.WebkitBackgroundClip = 'padding';
				e.style.WebkitBoxShadow = '6px 6px 6px rgba(0,0,0,0.4)';
			} else if (/gecko/i.test(nUa)){
				e.style.MozBackgroundClip = 'padding';
				e.style.MozBoxShadow = '6px 6px 6px rgba(0,0,0,0.4)';
			}
		}
		p = null;
	} catch(err){ }
	d = document.createElement('div');
	e.appendChild(d);
	d.id = 'ajaxWrap';
	
	i = document.createElement('span');
	e.appendChild(i);
	i.id = 'ajaxImg';
	i.innerHTML = '<span id="ajaxMin">_</a><span id="ajaxClose">&#10006;</a>';
	i.onclick = pclose;
	
	o = document.createElement('div');
	d.appendChild(o);
	o.id = 'ajaxHTML';
}
//Ajax dialog / popup function
function appendPopup(el){
	if($.ajax){
		a = $(el).find('.dialog').each(function(i,o){
			o = $(o);
			
			if (o.data('ran')) return;
			
			h = o.attr('href').replace('http://www.wyavtv.org','');

			if (h.indexOf('?src=')!=-1 && !o.hasClass('refresh'))
				h = h.substring(0,h.indexOf('?src='));

			o.data('ajaxURL',(h+(h.match(/\?/)?'&':'?')+'mode=ajax'));
					
			o.click(function(e){
				e.preventDefault();
				$.ajax({
					type:"GET",
					beforeSend: function(e){
						$('#loading').fadeIn(200);
					},
					url: $(this).data('ajaxURL'),
					success: function(msg){
						loadAjax(msg);
						appendPopup(document.getElementById('ajaxHTML'));
						return;
					}
				});
				return false;
			});
			
			o.data('ran',1);
		});
	} else {
		a = $(el).find('.dialog').each(function(i,o){
			o = $(o);
			
			h = o.attr('href').replace('http://www.wyavtv.org','');
			if (h.indexOf('?src=')!=-1)
				h = h.substring(0,h.indexOf('?src='));

			o.data('diagURL', h+(h.match(/\?/)?'&':'?')+'mode=popup');

			o.click(function(e){
				return mkPopup($(this).data('diagURL'),700,400,'win'+Math.floor(Math.random()*99999));
			});
		});
	}
	
	a = $(el).find('.accountTask').each(function(i,o){
		o = $(o);
		
		h = o.attr('href').replace('http://www.wyavtv.org','');
		if (h.indexOf('?src=')!=-1)
			h = h.substring(0,h.indexOf('?src='));

		o.data('acURL', h+(h.match(/\?/)?'&':'?')+'mode=popup');
		
		o.click(function (e){
			return mkPopup($(this).data('acURL'),700,400,'accountTask');
		});
	});
	
	a = $(el).find('.popup').each(function(i,o){
		o = $(o);
		
		h = o.attr('href').replace('http://www.wyavtv.org','');
		if (h.indexOf('?src=')!=-1)
			h = h.substring(0,h.indexOf('?src='));

		o.data('popURL', h+(h.match(/\?/)?'&':'?')+'mode=popup');
		
		o.click(function (e){
			return mkPopup($(this).data('popURL'),700,400,'win'+Math.floor(Math.random()*99999));
		});
	});
}
$(function(){appendPopup(document)});

function loadAjaxComments(){
	a = $(document).find('#video_comments a').each(function(i,o){
		o = $(o);
		h = o.attr('href').replace('http://www.wyavtv.org','');

		if (h.indexOf('?src=')!=-1 && !o.hasClass('refresh'))
			h = h.substring(0,h.indexOf('?src='));

		o.data('url', h+(h.match(/\?/)?'&':'?')+'mode=ajax');

		o.click(function(e){
			e.preventDefault();
			$.ajax({
				type:"GET",
				beforeSend: function(e){
					$('#loading').fadeIn(200);
				},
				url: $(this).data('url'),
				success: function(msg){
					$('#video_comments').html(msg)
					$('#loading').fadeOut(200);
					loadAjaxComments();
					return;
				}
			});
			return false;
		});
	});
}
$(function(){loadAjaxComments()});

//Page Ajax Load function
function replaceAjax(el){
	if($.ajax){
		a = $(el).find('.content').each(function(i,o){
			o = $(o);

			h = o.attr('href').replace('http://www.wyavtv.org','');
			if (h.indexOf('?src=')!=-1)
				h = h.substring(0,h.indexOf('?src='));
			
			o.data('url', h+(h.match(/\?/)?'&':'?')+'mode=ajax');

			o.click(function(e){
				e.preventDefault();
				loadContent($(this).data('url'),true);
				return false;
			});
		});
	}
}
$(function(){replaceAjax(document)});

//Ajax: Back/forth navigation restoration
if ("pushState" in history) {
	window.onpopstate = function(e){
		if ($(document).data('loaddate')){
			diff = new Date().getTime() - $(document).data('loaddate');
			if (diff < 3000 && diff > 0) return;
			else
				$(document).data('check',null);
		}
		if (e.state == null) h = window.location.pathname+(window.location.search?window.location.search+'&':'?')+'mode=ajax';
		else h = e.state.url;
		loadContent(h,true,true);
	}
	window.addEventListener('load',function(e){
		$(document).data('loaddate',+new Date());
	},false);
} else if ("onhashchange" in window) {
	window.onhashchange = function(e){
		var h = window.location.hash;
		if(h.substring(0,2) != '#!') return;
		h = h.substring(2);
		if ($(document).attr('oldHash') == h || (h=='' && $(document).attr('oldHash')==null)) return; // using attr('something') returns null when attr('something','')  but null != ''
		if (h=='') h = window.location.pathname+(window.location.search?window.location.search+'&':'?')+'mode=ajax';
		loadContent(h,true);
	}
} else {
	setInterval(function(){
		var h = window.location.hash;
		if(h.substring(0,2) != '#!') return;
		h = h.substring(2);
		if ($(document).attr('oldHash') == h || (h=='' && $(document).attr('oldHash')==null)) return; // using attr('something') returns null when attr('something','')  but null != ''
		if (h=='') h = window.location.pathname+(window.location.search?window.location.search+'&':'?')+'mode=ajax';
		loadContent(h,true);
	}, 1000);
}
//Ajax: Refresh page restoration
var h = window.location.hash;

if (h.substring(1,2) != '!' || h == ''){
	$(function(e){
		try{pageTracker._trackPageview(window.location.href.replace('http://www.wyavtv.org',''))}catch(e){}
	});
}

if(h.substring(0,2) == '#!'){
	h = h.substring(2);
	h = h.replace('?mode=ajax','');
	h = h.replace('&mode=ajax','');
	if (h.substring(0,1)=='/')
		window.location.href = 'http://www.wyavtv.org'+h;
}
//Ajax RPR END.

function ajaxFormSubmit(e){
	e.preventDefault();
	d = $(this).serialize();
	if (d.length)
		d += '&mode=ajax';
	else
		d += 'mode=ajax';

	if ($(this).attr('action') == '') { return }
	
	$.ajax({
		url: $(this).attr('action'),
		beforeSend: function(){
			$('#loading').fadeIn();
		},
		type:$(this).attr('method'),
		data: d,
		success:function(msg){
			loadAjax(msg);
		}
	});
	return false;
}

function pclose(){
	if ($('#ajax').length){
		$("#ajax").fadeOut(200);
		if (document.all)		
			$("#ajaxImg").hide();
		else
			$("#ajaxImg").fadeOut(200);
	}
	return false;
}

function loadAjax(msg){
	if (!document.getElementById('ajax')){ createAjax(); }
	if ($('#ajax').css('display') == 'none'){
		$('#ajaxImg').fadeIn();
		$('#ajax').fadeIn();
		$('#ajax').draggable({handle:'h1'});
	}
	$('#ajaxHTML').html(msg);
	if ($('#bodyID').html()){
		$('#ajaxHTML').html('<div id="'+$('#bodyID').html()+'">'+msg+'</div>');
		$('#bodyID').remove();
	}
	$('#ajaxWrap').resizable("destroy").resizable({alsoResize: '#ajax'});
	$('.ui-icon-gripsmall-diagonal-se').css('zIndex','100000');
	$('#ajaxHTML form').submit(ajaxFormSubmit);
	$('#loading').fadeOut(200);
	focusAjax();
}
function loadContent(href,track,noPush){
	$.ajax({
		beforeSend: function(){
			$('#loading').fadeIn();
		},
		type:"GET",
		url: href,
		success: function(msg){
			try{
				if ($('.tiles_new').length){
					$('.vt_name').ellipsis(true);
					$('.vt_tooltip').show();
					$('.vtt_des').css('overflow','hidden').height(60).ellipsis(true).height('auto').css('overflow','visible');
					$('.vt_tooltip').hide();
					$('.v_tile').hoverIntent({over:function(e){$('.vt_tooltip',this).fadeIn(100)},timeout: 500,out:function(e){$('.vt_tooltip',this).fadeOut(100)}});
				}
				if(!$.featureVechicle.pause){
					$.featureVechicle.pause = true;
					$.featureVechicle.interval = clearInterval($.featureVechicle.interval);
				}
			} catch (e) {};
			$('#content').html(msg);

			document.body.id = $('#bodyID').html();
			$('#bodyID').remove();
			
			$('#loading').fadeOut(200);
			replaceAjax(document.getElementById('content'));
			appendPopup(document.getElementById('content'));
			k = this.url.replace('?mode=ajax','');
			k = k.replace('&mode=ajax','');
			$('#logout').attr('href','/logout.php?src='+escape(k));
			if ("pushState" in history) {
				if (!noPush)
					history.pushState({url: this.url}, $('#title').html(), this.url.replace('?mode=ajax','').replace('&mode=ajax',''));
			} else {
				document.title = $('#title').html();
				window.location.hash = '#!'+this.url;
				$(document).attr('oldHash',window.location.hash.substring(2));
			}
			if (track) {try{pageTracker._trackPageview(this.url)}catch(e){}}
		},
		error: function(obj,status,error){
			$('#content').html('An error occured: '+obj.status+': '+obj.statusText);
			$('#loading').fadeOut(200);
		}
	});
}

function focusAjax(){
	$('#ajax').removeClass('minimized');
	if ($(window).scrollLeft() > $('#ajax').offset().left || 
			$(window).scrollLeft() + $(window).width() < $('#ajax').offset().left)
		$('#ajax').css('left',($(window).width() - 720) / 2 + 'px')
	if ($(window).scrollTop() > $('#ajax').offset().top || 
			$(window).scrollTop() + $(window).height() < $('#ajax').offset().top)
		$.scrollTo($('#ajax').offset().top-20, 200);
	
	$('#ajax').height('auto');
	if ($('#ajax').height() > ($(window).height() - 60)){
		$('#ajax').height($(window).height() - 60);
	}
}

function smallHeader(){
	$('body').addClass('small-header');
}
function restoreHeader(){
	$('body').removeClass('small-header');
}

$(function(e){$('#video_share a').addClass('popup')});
$(function(e){appendPopup(document.getElementById('video_share'))});
$(function(e){if($('#menu').offset().top>120){$('#masthead').css('margin','15px 5px 0 0')}}); //Fix Google Chrome
$(function(e){$('#s_name').click(function(e){$('#s_task').toggle(200)}).hover(function(e){$(this).addClass('button')},function(e){/*$(this).removeClass('button')*/})});

(function() {
	var apngTest = new Image(),
	ctx = document.createElement("canvas").getContext("2d");
	apngTest.onload = function () {
		ctx.drawImage(apngTest, 0, 0);
		apng_supported = ( ctx.getImageData(0, 0, 1, 1).data[3] === 0 );
		if (typeof apng_supported !== "boolean") {
			apng_supported = false;
		}
		if (apng_supported){
			$.cookie('apng',true);
		}
	};
	apngTest.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACGFjVEwAAAABAAAAAcMq2TYAAAANSURBVAiZY2BgYPgPAAEEAQB9ssjfAAAAGmZjVEwAAAAAAAAAAQAAAAEAAAAAAAAAAAD6A+gBAbNU+2sAAAARZmRBVAAAAAEImWNgYGBgAAAABQAB6MzFdgAAAABJRU5ErkJggg==";
}());
$(function(e){if ($('.tiles_new').length){
	$('.v_tile').each(function(e){
		if ($(this).data('ellipsis')) return;
		
		$('.vt_name',this).ellipsis(true);
		$('.vt_tooltip',this).show();
		$('.vtt_des',this).css('overflow','hidden').height(60).ellipsis(true).height('auto').css('overflow','visible');
		$('.vt_tooltip',this).hide();
		$(this).hoverIntent({over:function(e){$('.vt_tooltip',this).fadeIn(100)},timeout: 500,out:function(e){$('.vt_tooltip',this).fadeOut(100)}}).data('ellipsis',true);
	});
}});

window.resize = focusAjax