// -------------------------------------------------------------------------------//
// START PAGE                                                                     //
// -------------------------------------------------------------------------------//

function startpage() {	
	if (navigator.userAgent.toLowerCase().indexOf('msie 6') > -1) {
		supersleight.run();
	}
	fixSideBar();
	if(navigator.userAgent.toLowerCase().indexOf('msie 7') > -1) {
		jQueryDetection('.maintab_back').css('padding','2px 0px 2px 0px');
		jQueryDetection('.mirrortab_back').css('padding','4px 0px 4px 0px');
	}
}

// -------------------------------------------------------------------------------//
// GOOGLE ANALYTICS                                                               //
// -------------------------------------------------------------------------------//

if ((TrackingCode == undefined) || (TrackingCode == "")){
	var TrackingCode = "UA-10396964-1";
}

if ((ClientCode == undefined) || (ClientCode == "")){
	var ClientCode = "none";
}

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
var hndl = window.setTimeout("StartTracking()", 100);

function StartTracking(){
if (typeof(_gat) == 'object')
{
window.clearTimeout(hndl);
	var firstTracker = _gat._getTracker(TrackingCode);
	firstTracker._initData();
	firstTracker._trackPageview();
	if(ClientCode != "none"){
		var secondTracker = _gat._getTracker(ClientCode);
		secondTracker._setDomainName("none");
		secondTracker._setAllowLinker(true);
		secondTracker._initData();
		secondTracker._trackPageview();
	}
} else {
hndl = window.setTimeout("StartTracking()", 1000);
}
}

// -------------------------------------------------------------------------------//
// GOOGLE ANALYTICS - LINK TRACKING                                               //
// -------------------------------------------------------------------------------//
function LinkTracking(address, category, action, label){
	var firstTracker = _gat._getTracker(TrackingCode);
	firstTracker._trackEvent(category, action, label);
	if(ClientCode != "none"){
		var secondTracker = _gat._getTracker(ClientCode);
		secondTracker._trackEvent(category, action, label);
	}
	setTimeout(function(){
		window.open(address.href);
		return false;
	}, 100);
}

// -------------------------------------------------------------------------------//
// CHECK TERMS AND CONDITIONS                                                     //
// -------------------------------------------------------------------------------//
function checkTacs() {
	tacs = document.getElementById('tacs');
	if (tacs.checked == 1) {
		return true;
	} else {
		alert("You must agree to our Terms and Conditions before you can join the Community.");
		return false;
	}
}

// -------------------------------------------------------------------------------//
// PNG TRANSPARENCY                                                               //
// -------------------------------------------------------------------------------//
var supersleight	= function() {	
	var root = false;
	var applyPositioning = true;
	
	// Path to a transparent GIF image
	var shim			= 'includes/javascript/clear.gif';
	
	// RegExp to match above GIF image name
	var shim_pattern	= /includes\/javascript\/clear\.gif$/i;	
	
	var fnLoadPngs = function() { 
		if (root) {
			root = document.getElementById(root);
		}else{
			root = document;
		}
		for (var i = root.all.length - 1, obj = null; (obj = root.all[i]); i--) {
			// background pngs
			if (obj.currentStyle.backgroundImage.match(/\.png/i) !== null) {
				bg_fnFixPng(obj);
			}
			// image elements
			if (obj.tagName=='IMG' && obj.src.match(/\.png$/i) !== null){
				el_fnFixPng(obj);
			}
			// apply position to 'active' elements
			if (applyPositioning && (obj.tagName=='A' || obj.tagName=='INPUT') && obj.style.position === ''){
				obj.style.position = 'relative';
			}
		}
	};

	var bg_fnFixPng = function(obj) {
		var mode = 'scale';
		var bg	= obj.currentStyle.backgroundImage;
		var src = bg.substring(5,bg.length-2);
		if (obj.currentStyle.backgroundRepeat == 'no-repeat') {
			mode = 'crop';
		}
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')";
		obj.style.backgroundImage = 'url('+shim+')';
	};

	var el_fnFixPng = function(img) {
		var src = img.src;
		var objClass = img.className;
		if(objClass.indexOf('hiddenElement') > -1){
			var tmpImg = new Image();
			tmpImg.src = img.src;			
			img.style.width = tmpImg.width + "px";
			img.style.height = tmpImg.height + "px";
			delete tmpImg;
		} else {
			img.style.width = img.width + "px";
			img.style.height = img.height + "px";
		}
		
		img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
		img.src = shim;
	};
	
	var addLoadEvent = function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			};
		}
	};
	
	return {
		init: function() { 
			addLoadEvent(fnLoadPngs);
		},
		
		limitTo: function(el) {
			root = el;
		},
		
		run: function() {
			fnLoadPngs();
		}
	};
}();

// -------------------------------------------------------------------------------//
// FIX SIDEBAR POSITIONING / HEIGHT                                               //
// -------------------------------------------------------------------------------//
function fixSideBar(){
	var contentHldr = jQueryDetection('#content');
	var sidebarHldr = jQueryDetection('#sidebar');
	var shareHldr = jQueryDetection('.widget_sexy_bookmarks_sidebar_widget');
	var contentHeight = contentHldr.height();
	var sidebarHeight = sidebarHldr.height();
	var shareHeight = shareHldr.height();

	// IF SHARE IS IN THE SIDEBAR, ADD HEIGHT BECAUSE OF ITS EXPANDED SIZE
	if(shareHeight > 0){
		sidebarHeight += 128;
	}
	
	if(document.location.href.indexOf("/community/messages") > 0) {
		var container = jQueryDetection('#container');
		containerHeight = container.height();
		containerHeight += 252;
	}
	
	// IF THE PAGE HAS AN IMAGE AT THE TOP, CHANGE THE OFFSET FOR WHERE THE SIDEBAR SHOULD APPEAR	
	if(jQueryDetection('.post-image-container').outerHeight(true) > 0){
		var sidebarOffset = jQueryDetection('.post-image-container').outerHeight(true);
	} else {
		var sidebarOffset = 0;
	}

	// SET THE MINIMUM CONTENT HEIGHT
	contentHldr.css("min-height",sidebarHeight);
	
	// SET THE CONTENTHEIGHT / SIDEBAR HEIGHT DEPENDING ON WHICH IS BIGGER
	if(contentHeight > sidebarHeight){
		//sidebarHldr.css("height",contentHeight-sidebarOffset);
	} else if(sidebarHeight > contentHeight){
		contentHldr.css("height",sidebarHeight);
	}
	
	if(document.location.href.indexOf("/community/messages") > 0) {
		container.css("height",containerHeight);
	}
	
	// POSITION THE SIDEBAR DEPENDING ON THE SIDEBAR OFFSET
	sidebarHldr.css("top",sidebarOffset);
	
	// SHOW THE SIDEBAR, POSITIONING HAS BEEN FIXED
	sidebarHldr.css("visibility","visible");
}
