

// JavaScript Document
jQuery(document).ready(function() {

	// init round corners
	$("div.homeslide").corner("cc:#fff");
	$("#profile-page").corner("cc:#fff");
	$("div.registerform").corner("cc:#fff");
	$("div.latestnews").corner("cc:#fff");
	$("div.login-newsticker").corner("cc:#fff");
	$("div.latest-profiles").corner("cc:#fff");
	$("div.searchsidebox").corner("cc:#fff");
	$("div.gallery-container").corner("cc:#fff");
	$("div.rounded").corner("cc:#fff");
	$("div.horoscope-ticker").corner("cc:#fff");
	$("div.loginform").corner("cc:#fff");
	$('div.bottomlinks ul li:last').css({background: "none", paddingRight: "0"})
	$("div.rounded2").corner();
	
	// init cycle sliders
	if($('#slides').get(0)) $('#slides').cycle({ 
		fx:	 'scrollHorz', 
		speed:  'slow',
		timeout: 0,
		next:   '#slidenext', 
		prev:   '#slideprev'
	});
	
	if($('div.horoscobeslide').get(0)) $('div.horoscobeslide').cycle({ 
		fx:	 'scrollHorz', 
		speed:  'slow', 
		timeout: 8000,
		next:   '#horoscobenext', 
		prev:   '#horoscobeprev'
	});
	if($('#tvslides').get(0))$('#tvslides').cycle({ 
		fx:     'scrollHorz', 
		speed:  'slow', 
		timeout: 8000,
		next:   '#tvslidesnext', 
		prev:   '#tvslidesprev'
	});

	if($('#scrolnews div.smallnews').get(0))$('#scrolnews div.smallnews').click(function(){
		var $shownews = $(this).attr("id")					   
		$('#newsslidebig div.newstipshwoen').hide();
		$('#newsslidebig div.'+$shownews).fadeIn();
	});

	if($('#newstrip_big div.smallnews').get(0))$('#newstrip_big div.smallnews').click(function(){
		var $shownews = $(this).attr("id")					   
		$('#newstrip_big .singlenewpage').hide();
		$('#newstrip_big .'+$shownews).fadeIn();
	});

	if($('#scrolnews').get(0))$('#scrolnews').cycle({ 
		fx:	 'scrollHorz', 
		speed:  'slow', 
		timeout: 5000, 
		next:   '#next', 
		prev:   '#prev'
	});

	$("div.leftslides").hover(
		function () {
			$(".vishide").fadeIn();
		},
		function () {
			$(".vishide").hide();
		}				   
	);
	
	// init nav tabs
	$("#navlinks ul.navigation a").click(function(e){
		var contID = '#' + $(this).attr('id') + '-links';
		
		if( $(this).parent().hasClass('active') ){
			$("#navlinks div.topnavsublinks").hide();
			$("#navlinks ul.navigation li").removeClass('selected');
	
			$(this).parent().addClass('selected');
			$(contID).show();
			return false;
		}
		
		if( $(this).attr('href').match(/\#/i) ){
			$(this).attr('href', $(contID).find('a:first').attr('href') );
		}
	});
	$("#navlinks ul.navigation li.active a").click();

	//initWhatsHotSliders();
	
	//loader for more button on arabic page
	$("#arabic div.more a").click(function(){
		//alert('ololo');
		//return false;
		$('#arabic div#hot-last-week').load(
			$(this).attr('href')
		);
		$(this).parent().html('loading...');
		return false;
	});

	$("#tabs").tabs({
		event: 'click',
		//cache: true,
		spinner: 'loading...',
		select: function(event, ui){
		},
		show: function(event, ui){
			initWhatsHotSliders( ui.panel );
			initWhatsHotPlaces( ui.panel );
		},
		ajaxOptions: { 
			type: 'POST',
			data: 'ajax=1'
		},
		fxAutoHeight: true
	});
	
});

function initWhatsHotPlaces(container){
	if(container == null) container = $('#tabs');
	var $places = $(container).find('#whats-hot-clubs');
	if(!$places.get(0)) return;
	
	$places.find('a').each(function(i, link){
		var $link = $(link);
		$link.attr('_href', $link.attr('href')).attr('href', '#show-photos');
		$link.click(function(){
			var photosURL = $link.attr('_href');
			$link.parents('#whats-hot-clubs').html('<div class="loading" style="width:100%;height:300px;"></div>');
			$.ajax({
				url:photosURL,
				success:function(response){
					$('#whats-hot-clubs').html(response);
					initWhatsHotSliders( $('#whats-hot-clubs') );
				}
			});
			return false;
		})
	});
}

function initWhatsHotSliders(container){
	
	if(container == null) container = $('#tabs');
	
	// what's hot sliders
	$(container).find('div.profileslides').each(function(){
		var class_match = this.className.match(/\sn(\d+)$/);
		var unique_id = class_match[1];
		$(this).cycle({
			fx:	 'scrollHorz', 
			speed:  'slow', 
			timeout: 0, 
			next:   '#next'+unique_id, 
			prev:   '#prev'+unique_id
		});
	});
	
	$(container).find('div.more a').each( function(i){
		$(this).attr('action', $(this).attr('href'))
			.attr('href', '#view-more')
			.click(function(){
				var tabContainer = $(this).parents('div.ui-tabs-panel');
				tabContainer.load(
					$(this).attr('action'),
					'',
					function(){
						initWhatsHotSliders( tabContainer );
					}
				);
				$(this).parent().html('loading...');
				return false;
			});
	});
}




//buble (tool tip)
$(function () {
		$('#profile-page li.bubbleInfo').each(function () {
			var top = -80, left = -80, distance = 100;
			var time = 250;
			var hideDelay = 500;

			var hideDelayTimer = null;

			var beingShown = false;
			var shown = false;
			var trigger = $('.trigger', this);
			var info = $('div.popup, div.popup-small', this).css('opacity', 0);
			if (info.hasClass('popup-small')){
				var top = -40, left = -37, distance = 50;
			}


			$([trigger.get(0), info.get(0)]).mouseover(function () {
				$('#main div.splitcontier').css('z-index', 999);
				//$('#menu, #menu > li').css('z-index', 0);
				
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				if (beingShown || shown) {
					// don't trigger the animation again
					return;
				} else {
					// reset position of info box
					beingShown = true;

					info.css({
						top: top,
						left: left,
						display: 'block'
					}).animate({
						top: '-=' + distance + 'px',
						opacity: 1
					}, time, 'swing', function() {
						beingShown = false;
						shown = true;
					});
				}

				return false;
			}).mouseout(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					info.animate({
						top: '-=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						shown = false;
						info.css('display', 'none');
						$('#main div.splitcontier').css('z-index', 0);
					});

				}, hideDelay);

				return false;
			});
		});
	});
