// JavaScript Document

var xb =
{
	evtHash: [],

	ieGetUniqueID: function(_elem)
	{
		if (_elem === window) { return 'theWindow'; }
		else if (_elem === document) { return 'theDocument'; }
		else { return _elem.uniqueID; }
	},

	addEvent: function(_elem, _evtName, _fn, _useCapture)
	{
		if (typeof _elem.addEventListener != 'undefined')
		{
			if (_evtName == 'mouseenter')
				{ _elem.addEventListener('mouseover', xb.mouseEnter(_fn), _useCapture); }
			else if (_evtName == 'mouseleave')
				{ _elem.addEventListener('mouseout', xb.mouseEnter(_fn), _useCapture); } 
			else
				{ _elem.addEventListener(_evtName, _fn, _useCapture); }
		}
		else if (typeof _elem.attachEvent != 'undefined')
		{
			var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) + '::evt_' + _evtName + '::fn_' + _fn + '}';
			var f = xb.evtHash[key];
			if (typeof f != 'undefined')
				{ return; }
			
			f = function()
			{
				_fn.call(_elem);
			};
		
			xb.evtHash[key] = f;
			_elem.attachEvent('on' + _evtName, f);
	
			// attach unload event to the window to clean up possibly IE memory leaks
			window.attachEvent('onunload', function()
			{
				_elem.detachEvent('on' + _evtName, f);
			});
		
			key = null;
			//f = null;   /* DON'T null this out, or we won't be able to detach it */
		}
		else
			{ _elem['on' + _evtName] = _fn; }
	},	

	removeEvent: function(_elem, _evtName, _fn, _useCapture)
	{
		if (typeof _elem.removeEventListener != 'undefined')
			{ _elem.removeEventListener(_evtName, _fn, _useCapture); }
		else if (typeof _elem.detachEvent != 'undefined')
		{
			var key = '{FNKEY::obj_' + xb.ieGetUniqueID(_elem) + '::evt' + _evtName + '::fn_' + _fn + '}';
			var f = xb.evtHash[key];
			if (typeof f != 'undefined')
			{
				_elem.detachEvent('on' + _evtName, f);
				delete xb.evtHash[key];
			}
		
			key = null;
			//f = null;   /* DON'T null this out, or we won't be able to detach it */
		}
	},
	
	mouseEnter: function(_pFn)
	{
		return function(_evt)
		{
			var relTarget = _evt.relatedTarget;				
			if (this == relTarget || xb.isAChildOf(this, relTarget))
				{ return; }

			_pFn.call(this, _evt);
		}
	},
	
	isAChildOf: function(_parent, _child)
	{
		if (_parent == _child) { return false };
		
		while (_child && _child != _parent)
			{ _child = _child.parentNode; }
		
		return _child == _parent;
	}	
};
$(document).ready(function() { <!--document onready -->

$("#heatmaplink_open").click(function() {	$("#heatmaphelp").toggle('slow');});
$("#heatmaplink_close").click(function() {	$("#heatmaphelp").toggle('slow');});
$('#wrapper').columnize({ columns:3 , buildOnce:true});
if(!$('div[class*=category]').hasClass('dontsplit')){ //check the split setting
checkOverflow('first column','column');  // checking middle column
checkOverflow('column','last column');  // checking last column
}
$('#wrapper').removeClass('hidden');
$('#container').corner('tr tl 7px');
$("div[class*=story]").each(function(){
	   var prica = document.getElementById($(this).attr('id'));
	   xb.addEvent(prica, 'mouseenter', enter, false);
	   xb.addEvent(prica, 'mouseleave', leave, false);
	 });

if(gIsAdmin == true)
{
	$('.text').removeClass("text").addClass("textAdmin");
	/*
	var txtTeaserEditor = new punymce.Editor(
	{        
		id : 'teaserArea',        
		toolbar : 'bold'
	});
	*/	
}

}); <!--/document onready -->



function enter(e)
			{
			    if(!$('#'+this.id).children('.text').hasClass('open')){
			    
					var str=$('#'+this.id).attr("id");
	                var teaser_reference=str.replace(/story/,"teaser");
					var text_reference=str.replace(/story/,"text");
					
					var boja = $('#'+this.id).find("#livecolor").attr('value');
					
					$('#'+this.id).highlightFade({speed:400,start: boja,end:"#FFE5E5"});
					$('#'+teaser_reference).highlightFade({speed:400,start: boja,end:"#FFE5E5"});
					$('#'+text_reference).highlightFade({speed:400,start: boja,end:"#FFE5E5"});
				}
			}
			
function leave(e)
			{
				if(!$('#'+this.id).children('.text').hasClass('open')){
				var str=$('#'+this.id).attr("id");
                var teaser_reference=str.replace(/story/,"teaser");
                var text_reference=str.replace(/story/,"text");
				var boja = $('#'+this.id).find("#livecolor").attr('value');
				
				$('#'+this.id).highlightFade({speed:300,start:"#FFE5E5" ,end:boja});
				$('#'+teaser_reference).highlightFade({speed:300,start:"#FFE5E5" ,end:boja});
				$('#'+text_reference).highlightFade({speed:300,start:"#FFE5E5" ,end:boja});
				
				}
			}
function teaser(teaser_reference){
  
var str=$('#'+teaser_reference).attr("name");
var incUrl=str.replace(/teaser/, gIncreaseClicksURL);
//var incUrl=str.replace(/teaser/, "http://localhost:8080/Heatmap/index.php/welcome/increaseClicks/");
var incUrl=str.replace(/teaser/, "http://times2.hitcreative.com/index.php/welcome/increaseClicks/");
var text_reference=str.replace(/teaser/, "text");
var story_reference=str.replace(/teaser/,"story");
if($('#'+text_reference).hasClass('open')){
//closeAll();
//over_effects(story_reference);
}else{

closeAll();
revealStory(story_reference,text_reference,teaser_reference,incUrl);

}
};

function closeAll(orgColor){
$("div[class*=story]").each(function (){ 
if($(this).children('.text').hasClass('open')){
var boja=$(this).find("#livecolor").attr('value');
$(this).highlightFade({speed:300,start:"#FFE5E5" ,end:boja});
$(this).children('.teaser').highlightFade({speed:300,start:"#FFE5E5" ,end:boja});
}
});
$('div[class*=open]').hide().removeClass('open'); <!--Close and debrand-->
$('div[class*=white]').removeClass('white'); <!--Remove white border-->
$('div[class*=hoverclass_with_b]').removeClass('hoverclass_with_b');
$('div[class*=hoverclass_without_b]').removeClass('hoverclass_without_b');
};




function over_effects(story){
if(!$('#'+story).children('.text').hasClass('open')){
//$('#'+story).children().toggleClass('hoverclass_with_b');
}
};


function revealStory(story_reference,text_reference,teaser_reference,incUrl){
var request=getHTTPObject();
request.open("GET",incUrl,true);
request.send(null);
$('#'+teaser_reference).addClass('hoverclass_without_b');
$('#'+text_reference).addClass('hoverclass_without_b');
$('#'+story_reference).addClass('white');
$('#'+text_reference).css('opacity','0').slideDown('slow').addClass('open');
showPerBrowser(text_reference);

};

function checkOverflow(prev,curr){

if(!$('div[class='+curr+'] div:eq(2)').hasClass('cattitle')){
	
	var category_name=$('div[class='+prev+'] > div.category:last #cat_title').text();
	
	
	$('div[class='+curr+'] div:eq(0)').prepend('<div id="cat_title" class="cattitle">'+category_name+'</div>');

}
};
function getHTTPObject(){
	var xhr=false;
	if(window.XMLHttpRequest){
		xhr=new XMLHttpRequest();
	} else if (window.ActiveXObject){
		try{
			xhr=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try{
				xhr=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e){
				xhr=false;
			}
		}
	}
	return xhr;
		
}

function showPerBrowser(text_reference){
var agent= navigator.userAgent.toLowerCase();
var ver = parseInt(navigator.appVersion);
 
var ie = agent.indexOf("msie")>=0;
var ie6=ie && agent.indexOf("msie 6")>=0;
var ie7=ie && agent.indexOf("msie 7")>=0;
 
var ff=!ie && agent.indexOf("mozilla")>=0;
var ff2=ff && ver==4;
var ff3=ff && ver==5;
 
if (ie6){
$('#'+text_reference).css('opacity','100');
} else if (ie7){
$('#'+text_reference).css('opacity','100');
}else{
$('#'+text_reference).animate(
{
opacity : '100'
},
2000);
;
}

}

