/*
* chatwindow.js
* A little chat box slidy kind of thing, the less-annoying version.
* There is server-side code that works with this to prevent displaying this box on every page.
* Written by Jeff Parker - jeffreyp(at)codero.com
* Props to the jQuery community for guidance and support
*
* $Id: chatwindow.js 5856 2011-06-07 16:36:18Z mjaning $
*/

/*
 * All new 2011 version by Marcelino Janing
 * 2011-04-15 22:51:00 Stable Version 1.4 Released
 */

/*
 * Chat Slide Over - Behavior and Effects - (Top Left Corner - Horizontal Slider)
 */
posexpand=0;
poscollapse=-213;
poshide=-264;
// showPopupChat=(typeof(showPopupChat)!='undefined') ? showPopupChat : false;
if(typeof(pageviewNum)!='undefined') {
	showPopupChat = (pageViewsTotal == 1) ? true : false;
}

var chat = {
	target: {
		slide:'#pricematch-slider'
		,tab:'#pricematch-slider a#pricematch-slider-tablink'
		,closex:''
		,nothanks:''
		,chatnow:'#pricematch-slider-uplink'
	}
	,poscollapse:poscollapse
	,posexpand:posexpand
	,poshide:poshide
	,oninit:{
		expand:{
			apply:showPopupChat,effects:{runtime:3000,delay:5000,css:{left:posexpand}}
			,autocollapse:{apply:true,effects:{runtime:3000,delay:13000,css:{left:poscollapse}}}
		}
		,collapse:{
			apply:false,effects:{runtime:2500,delay:10000,css:{left:poscollapse}}
			,autoexpand:{apply:false,effects:{runtime:3000,delay:15000,css:{left:posexpand}}}
		}
		,hide:{
			apply:false,effects:{runtime:3500,delay:15000,css:{left:poscollapse}}
			,autoexpand:{apply:false,effects:{runtime:3000,delay:5000,css:{left:posexpand}}}
		}
	}
	,onclick:{
		expand:{
			apply:true,effects:{runtime:1000,delay:0,css:{left:posexpand}}
			,autocollapse:{apply:false,effects:{runtime:1500,delay:8000,css:{left:poscollapse}}}
		}
		,collapse:{
			apply:true,effects:{runtime:1000,delay:0,css:{left:poscollapse}}
			,autoexpand:{apply:false,effects:{runtime:3000,delay:5000,css:{left:posexpand}}}
		}
		,hide:{
			apply:true,effects:{runtime:500,delay:0,css:{left:poscollapse}}
			,autoexpand:{apply:false,effects:{runtime:3000,delay:5000,css:{left:posexpand}}}
		}
	}
	,onmouseover:{
		expand:{
			apply:false,effects:{runtime:2500,delay:0,css:{left:posexpand}}
			,autocollapse:{apply:true,effects:{runtime:2500,delay:5000,css:{left:poscollapse}}}
		}
		,collapse:{
			apply:false,effects:{runtime:2000,delay:0,css:{left:poscollapse}}
			,autoexpand:{apply:false,effects:{runtime:2500,delay:5000,css:{left:posexpand}}}
		}
		,hide:{
			apply:false,effects:{runtime:500,delay:0,css:{left:poscollapse}}
			,autoexpand:{apply:false,effects:{runtime:3000,delay:9000,css:{left:poscollapse}}}
		}
	}
	,onhover:{
		expand:{
			apply:true,effects:{runtime:1500,delay:400,css:{left:posexpand}}
			,autocollapse:{apply:true,effects:{runtime:1500,delay:7500,css:{left:poscollapse}}}
		}
		,collapse:{
			apply:false,effects:{runtime:2000,delay:1500,css:{left:poscollapse}}
			,autoexpand:{apply:false,effects:{runtime:2500,delay:5000,css:{left:posexpand}}}
		}
		,hide:{
			apply:false,effects:{runtime:500,delay:1500,css:{left:poscollapse}}
			,autoexpand:{apply:false,effects:{runtime:3000,delay:9000,css:{left:poscollapse}}}
		}
	}
};

// asset function, call it when you need debug or compose a new behaviors, e.g. alert(mypos());
function mypos() {
	var chatpos = $(chat.target.slide).position();
	return ('top:' + chatpos.top + '|right:' + chatpos.right + '|bottom:' + chatpos.bottom + '|left:' + chatpos.left );
}
// helper for testing if slide is considered as fully off, result depends on the poscollapse var definition
function iscollapsed() {
	var chatpos = $(chat.target.slide).position();
	return ((chatpos.top<=chat.poscollapse)||(chatpos.right<=chat.poscollapse)||(chatpos.bottom<=chat.poscollapse)||(chatpos.left<=chat.poscollapse));
}
// helper for testing if slide is at least partialy visible
function isexpanded() {
	var chatpos = $(chat.target.slide).position();
	return (((chatpos.top>=0)||(typeof(chatpos.top)=='undefined'))&&
		      ((chatpos.right>=0)||(typeof(chatpos.right)=='undefined'))&&
					((chatpos.bottom>=0)||(typeof(chatpos.bottom)=='undefined'))&&
					((chatpos.left>=0)||(typeof(chatpos.left)=='undefined')));
}
// helper for testing if slide is at least partialy visible
function isvisible() {
	var chatpos = $(chat.target.slide).position();
	return ((chatpos.top>=0)||(chatpos.right>=0)||(chatpos.bottom>=0)||(chatpos.left>= 0));
}
// jQuery animation stuff
function animate(effects,action) {
	$(chat.target.slide).animate(effects.css, effects.runtime, function(){
		// at 20110606 by Marcelino - trackEvent affects bounce rate because GA take it as page view
		//_gaq.push(['_trackEvent', 'Live Chat', 'Popup Window', action, pageviewNum]);
	});
}
$(document).ready(function() {
	var a=null;
	var e=null;
	var c=null;
	var h=null;
	// remore all href attribute that are kind of empty
	$(chat.target.slide + ' a[href=#]').removeAttr('href');
	
	if(chat.oninit.expand.apply) {
		clearTimeout(e);clearTimeout(a);
		e=setTimeout("animate(chat.oninit.expand.effects,'Initial Display')",chat.oninit.expand.effects.delay);
		if(chat.oninit.expand.autocollapse.apply) {
			a=setTimeout("animate(chat.oninit.expand.autocollapse.effects,'Auto Collapse')",chat.oninit.expand.autocollapse.effects.delay);
		}
	}
	if(chat.oninit.collapse.apply) {
		clearTimeout(c);clearTimeout(a);
		c=setTimeout("animate(chat.oninit.collapse.effects,'Initial Collapse')",chat.oninit.collapse.effects.delay);
		if(chat.oninit.collapse.autoexpand.apply) {
			a=setTimeout("animate(chat.oninit.collapse.autoexpand.effects,'Auto Expand')",chat.oninit.collapse.autoexpand.effects.delay);
		}
	}
	if(chat.oninit.hide.apply) {
		clearTimeout(h);clearTimeout(a);
		h=setTimeout("animate(chat.oninit.hide.effects,'Initial Hide')",chat.oninit.hide.effects.delay);
		if(chat.oninit.hide.autoexpand.apply) {
			a=setTimeout("animate(chat.oninit.hide.autoexpand.effects,'Auto Expand')",chat.oninit.hide.autoexpand.effects.delay);
		}
	}
	$(chat.target.closex).click(function() {
		if(chat.onclick.hide.apply && isexpanded()) {
			clearTimeout(h);clearTimeout(a);
			h=setTimeout("animate(chat.onclick.hide.effects,'Click Close X Button')",chat.onclick.hide.effects.delay);
			if(chat.onclick.hide.autoexpand.apply) {
				a=setTimeout("animate(chat.onclick.hide.autoexpand.effects,'Auto Expand')",chat.onlick.hide.autoexpand.effects.delay);
			}
		}
	});
	$(chat.target.nothanks).click(function() {
		if(chat.onclick.hide.apply && isexpanded()) {
			clearTimeout(h);clearTimeout(a);
			h=setTimeout("animate(chat.onclick.hide.effects,'Click No Thanks Button')",chat.onclick.hide.effects.delay);
			if(chat.onclick.hide.autoexpand.apply) {
				a=setTimeout("animate(chat.onclick.hide.autoexpand.effects,'Auto Expand')",chat.onlick.hide.autoexpand.effects.delay);
			}
		}
	});
	$(chat.target.chatnow).click(function() {
		if(chat.onclick.hide.apply && isexpanded()) {
			clearTimeout(h);clearTimeout(a);
			h=setTimeout("animate(chat.onclick.hide.effects,'Click Chat Now Button')",chat.onclick.hide.effects.delay);
			if(chat.onclick.hide.autoexpand.apply) {
				a=setTimeout("animate(chat.onclick.hide.autoexpand.effects,'Auto Expand')",chat.onlick.hide.autoexpand.effects.delay);
			}
		}
	});
	$(chat.target.tab).click(function() {
		if(chat.onclick.expand.apply && iscollapsed()) {
			clearTimeout(e);clearTimeout(a);
			e=setTimeout("animate(chat.onclick.expand.effects,'Click Expand')",chat.onclick.expand.effects.delay);
			if(chat.onclick.expand.autocollapse.apply) {
				a=setTimeout("animate(chat.onclick.expand.autocollapse.effects,'Auto Collapse')",chat.onlick.expand.autocollapse.effects.delay);
			}
		}
		if(chat.onclick.collapse.apply && isexpanded()) {
			clearTimeout(c);clearTimeout(a);
			c=setTimeout("animate(chat.onclick.collapse.effects,'Click Collapse')",chat.onclick.collapse.effects.delay);
			if(chat.onclick.collapse.autoexpand.apply) {
				a=setTimeout("animate(chat.onclick.collapse.autoexpand.effects,'Auto Expand')",chat.onclick.collapse.autoexpand.effects.delay);
			}
		}
		if(chat.onclick.hide.apply && isexpanded()) {
			clearTimeout(h);clearTimeout(a);
			h=setTimeout("animate(chat.onclick.hide.effects,'Click Hide')",chat.onclick.hide.effects.delay);
			if(chat.onclick.hide.autoexpand.apply) {
				a=setTimeout("animate(chat.onclick.hide.autoexpand.effects,'Auto Expand')",chat.onclick.hide.autoexpand.effects.delay);
			}
		}
	});
	$(chat.target.tab).mouseover(function() {
		if(chat.onmouseover.expand.apply && iscollapsed()) {
			clearTimeout(e);clearTimeout(a);
			e=setTimeout("animate(chat.onmouseover.expand.effects,'Mouse Over Expand')",chat.onmouseover.expand.effects.delay);
			if(chat.onmouseover.expand.autocollapse.apply) {
				a=setTimeout("animate(chat.onmouseover.expand.autocollapse.effects,'Auto Collapse')",chat.onmouseover.expand.autocollapse.effects.delay);
			}
		}
		if(chat.onmouseover.collapse.apply && isexpanded()) {
			clearTimeout(c);clearTimeout(a);
			c=setTimeout("animate(chat.onmouseover.collapse.effects,'Mouse Over Collapse')",chat.onmouseover.collapse.effects.delay);
			if(chat.onmouseover.collapse.autoexpand.apply) {
				a=setTimeout("animate(chat.onmouseover.collapse.autoexpand.effects,'Auto Expand')",chat.onmouseover.collapse.autoexpand.effects.delay);
			}
		}
		if(chat.onmouseover.hide.apply && isexpanded()) {
			clearTimeout(h);clearTimeout(a);
			h=setTimeout("animate(chat.onmouseover.hide.effects,'Mouse Over Collapse')",chat.onmouseover.hide.effects.delay);
			if(chat.onmouseover.hide.autoexpand.apply) {
				a=setTimeout("animate(chat.onmouseover.hide.autoexpand.effects,'Auto Expand')",chat.onmouseover.hide.autoexpand.effects.delay);
			}
		}
	});
	$(chat.target.tab).hover(
		function() {
			if(chat.onhover.expand.apply && iscollapsed()) {
				clearTimeout(e);clearTimeout(a);
				e=setTimeout("animate(chat.onhover.expand.effects,'Mouse Over Expand')",chat.onhover.expand.effects.delay);
				if(chat.onhover.expand.autocollapse.apply) {
					a=setTimeout("animate(chat.onhover.expand.autocollapse.effects,'Auto Collapse')",chat.onhover.expand.autocollapse.effects.delay);
				}
			}
			if(chat.onhover.collapse.apply && isexpanded()) {
				clearTimeout(c);clearTimeout(a);
				c=setTimeout("animate(chat.onhover.collapse.effects,'Hover Collapse')",chat.onhover.collapse.effects.delay);
				if(chat.onhover.collapse.autoexpand.apply) {
					a=setTimeout("animate(chat.onhover.collapse.autoexpand.effects,'Auto Expand')",chat.onhover.collapse.autoexpand.effects.delay);
				}
			}
			if(chat.onhover.hide.apply && isexpanded()) {
				clearTimeout(h);clearTimeout(a);
				h = setTimeout("animate(chat.onhover.hide.effects,'Hover Collapse')",chat.onhover.hide.effects.delay);
				if(chat.onhover.hide.autoexpand.apply) {
					a=setTimeout("animate(chat.onhover.hide.autoexpand.effects,'Auto Expand')",chat.onhover.hide.autoexpand.effects.delay);
				}
			}
		},
		function() {
			if(chat.onhover.expand.apply && iscollapsed()) {
				clearTimeout(e);clearTimeout(a);
			}
			if(chat.onhover.collapse.apply && isexpanded()) {
				clearTimeout(c);clearTimeout(a);
			}
			if(chat.onhover.hide.apply && isexpanded()) {
				clearTimeout(h);clearTimeout(a);
			}
		}
	);
});
