$(document).ready(function(){
	//TV AD
	var tmMapLoaded = false;
	$('.tmMap .mapButt').click(function(e){
		e.preventDefault(); 
		if ($('.tmMap').hasClass('active')) {
			$('.tmMap').removeClass('active').animate({marginTop: $('.tmMap').height()*-1},'slow');	
		} else {
			if (!tmMapLoaded) { $('.tmMap .mapContent').load('/tmMap');
				tmMapLoaded = true;
			}
			$('.tmMap').addClass('active').animate({marginTop: 0},'slow');
		}
	});
	
	$('.tmMap .closeLayer').click(function(e){
		e.preventDefault(); 
		$('.tmMap').removeClass('active').animate({marginTop: $('.tmMap').height()*-1},'slow');
	});	
	
	$('form.ajax').live('submit',function(e){
		var form = this;
		$(this).closest('div.popupContainer').load('/profile/ajaxLogin',$(this).serializeArray(),function(responseText, textStatus, XMLHttpRequest) {
			if (responseText == 'Loading') {
				login = true;
				$($(form).attr('rel')).click();
			}
																								 
		});
		return false;
	});
	
	$('a.popup_freetext').click(function(e){
		e.preventDefault(); 
		if (!$('#popup_freetext').length) {
			$("body").append('<div id="popup_freetext" class="popup" style="display:none;"></div>');	
		} 
		
		$('#popup_freetext').html('').show().addClass('loading').load($(this).attr('href'),function(response, status, xhr) {
			$('#popup_freetext').removeClass('loading');
		});
	 
	});
	
	$('#popup_freetext .close').live('click',function(e){
		e.preventDefault();
		$('#popup_freetext').hide();
	});
	
	$("a.flvVideo").click(function(e){
		e.preventDefault(); 
		var flv = $(this).attr("href");
		var container = $(this).attr("rel");
		var thumb = $(this).siblings('img').attr("src");
		swfobject.embedSWF("/swf/flvPlayer.swf?imagePath="+thumb+"&videoPath="+flv+"?r=558435&autoStart=true&autoHide=false&autoHideTime=5&hideLogo=true&volAudio=60&newWidth=320&newHeight=265&disableMiddleButton=false&playSounds=false&soundBarColor=0x0066FF&barColor=0xf10000&barShadowColor=0xc2c2c2&subbarColor=0xffffff", container, "320", "265", "9.0.0");								   
	});

    

	
	
	//Poll
	
	function pollVoteResults(value) {
		if (value) var p = {value: value};
			else var p = {};
		var pollId = $("#pollbox").attr("rel");
		$('#pollbox').addClass("loading").load('/polls/'+pollId,p,function(){$('#pollbox').removeClass("loading").addClass("results");});
	}
	$('#pollresult').click(function(e) {
		e.preventDefault();	
		pollVoteResults(false);
	});
	$('#popup_login .close').live('click',function(e){

		e.preventDefault();

		$('#popup_login').hide();
		
	});
	$('#pollvote').click(function(e) {
		$('#popup_login').hide();
		e.preventDefault();	
		if (!login) {
			$('#popup_login').show().find('.popupContainer').load('/profile/ajaxLogin',{rel: '#pollvote'});
			return false;
		}
		var value = $(this).closest('ul').find("input[name=poll_answer]:checked").val();
		if (typeof value == "undefined") {
			alert("You must select an option!");
			return false;
			
		}
		pollVoteResults(value);
	});
	
	$("ul.parent > li.mainmenu").live('mouseover',function(){
	  $(this).addClass('active').closest('div').css('background-color',$(this).attr("relcolor"));
	 
	});
	$("ul.parent > li.mainmenu").live('mouseout',function(){
	  $(this).removeClass('active').closest('div').css('background-color',$(this).closest('div.menus').attr("rel"));
	});
	
/*	var $liveTip = $('<div class="qtip" style="position:absolute;" id="livetip"></div>').hide().appendTo('body'),
    $win = $(window),
    showTip = false,
	activeItem = false;
	
	var tip = {
	  title: '',
	  offset: 12,
	  delay: 300,
	  position: function(event) {
		var positions = {x: event.pageX, y: event.pageY};
		var dimensions = {
		  x: [
			$win.width(),
			$liveTip.outerWidth()
		  ],
		  y: [
			$win.scrollTop() + $win.height(),
			$liveTip.outerHeight()
		  ]
		};
	
		for ( var axis in dimensions ) {
	
		  if (dimensions[axis][0] < dimensions[axis][1] + positions[axis] + this.offset) {
			positions[axis] -= dimensions[axis][1] + this.offset;
		  } else {
			positions[axis] += this.offset;
		  }
	
		}
	
		$liveTip.css({
		  top: positions.y,
		  left: positions.x
		});
	  }
	};
	
	$('div.item[rel]').live('mouseover',function(event){
		

		
		$link = $(this);
		var cItem;
		if (activeItem == $link.attr('rel')) {
			return false;
		}
		activeItem = $link.attr('rel');
		
		if (showTip) clearTimeout(showTip);
		showTip = setTimeout(function() {
	
		  $link.data('tipActive', true);
	
		  tip.position(event);
	
		  
		  $liveTip.load($link.attr("tooltip")).show();
	
		}, tip.delay);										 
		showTip = true;									 
												 
	});
	
	
	$('div.item[rel]').live('mouseover',function(event){
		
		
		$liveTip.html('').hide();
	 	if (showTip) clearTimeout(showTip);							 
		showTip = false;									 
												 
	});
	*/

	$('.qtip a.closeLayer').live('click',function(e){
		e.preventDefault()													  
		$(this).closest('.qtip').qtip('hide');
	});
	
//	$('div.item[tooltip]').live("mouseover", function(){
//		$('div.item[tooltip]').each(function()
//		{
//          if( $(this).data('qtip') ) { return true; }
//		  $(this).qtip(
//		  {
//			content:  {
//				url: $(this).attr('tooltip')
//			},
//			position: {
//				corner: {
//				   target: 'rightTop', // Position the tooltip above the link
//				   tooltip: 'leftTop'
//				},
//				adjust: {
//				   screen: true // Keep the tooltip on-screen at all times
//				}			
//			},
//			show : {
//				delay: 250,
//				solo: true
//				
//			}
//		  });
//		});
//    });	
//	$("div.products div.paginator a").live("click",function(event){
//		event.preventDefault();
//		var curPage = $(this).closest('ul').children(".active").removeClass("active").children("a").attr("rel");
//		var allPages = $(this).parent().siblings().length - 1;
//		var newPage = $(this).attr("rel");
//		
//		if (newPage == 'next') {
//			if (curPage == allPages) newPage = 1;	
//			else newPage = parseInt(curPage) + 1;
//		} else if (newPage == 'prev') {
//			if (curPage == 1) newPage = allPages;	
//			else newPage = curPage - 1;
//		}
//		var curCategory = $(this).closest('.products').siblings('.header').find('li.active a').attr("rel");
//		$(this).closest('div.products').addClass('loading').load("/newproducts/"+curCategory+"/"+newPage,false,function(){$(this).removeClass("loading")});
//	});
//	
//	$("div.header ul.subcats a").live("click",function(event){
//		event.preventDefault();
//		var curCategory = $(this).closest('ul').children(".active").removeClass("active").children("a").attr("rel");
//		var newCategory = $(this).attr("rel");
//		$(this).parent().addClass("active");
//		$(this).closest('.header').siblings('.products').addClass('loading').load("/newproducts/"+newCategory+"/1",false,function(){$(this).removeClass("loading")});
//	});	
});


