$(document).ready(function() {
	if (window.fixPNG) { fixPNG('.playbutton'); } // magic global function, additional pngs to fix
	
	$('.popupLink, a[rel~=popup]').live("click", function(event) {
		
		//Been changed Cause this line stop popup working in IE
		//if(event.button != ($.browser.msie ? 1 : 0) || event.ctrlKey || event.shiftKey || event.altKey) return; // bug: click bound to a live event detects right/middle buttons
		
		var url 	= event.target.href;
		var width 	= getURLParameter('w', url);
		var height 	= getURLParameter('h', url);
		var scroll 	= getURLParameter('s', url);
		openPopup(url, url, width, height, scroll);
		
		return false;
	});
	
	// plain new window
	$('a[rel~=new]').live("click", function() {
		window.open($(this).attr('href'));
        return false;
	});
	
	// add analytics tracking string to all external links
	$('a').click(function() { 		
		var lhref = $(this).attr('href');
		if(lhref.indexOf('http://') != -1) { // check it's an external link 
			//Replace all odd characters, so that it works with Analytics Navigation analysis  
			var url = lhref.replace(/[^a-z|A-Z]/g, "_");
			var txt = $(this).html().replace(/[^a-z|A-Z]/g, "_");
			var str = '/outgoinglink/-' + txt + '-' + url;
			
			try {
				pageTracker._trackPageview(str);  
			} catch(err) { 
			}  
		}
	});
});

// called from the carousel to open the ringtone popup window
function ringtone()
{
	openPopup('/popup/ringtone.php', '/popup/ringtone.php', '500', '600', 'no');
}

function openPopup(mypage,myname,w,h,scroll)
{
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
	
	var realname = myname.substring(1,4);
	
	window.open(mypage, realname, settings);
	return false;
}

function getURLParameter(name, url)
{
	url = (url != null)?url:window.location.href;
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
	if( results == null )
		return "";
	else
		return results[1];
}