/* ------------------------------------------------------------------- */

$.fn.extend({
	skatic_tooltip: function(tooltipContent, splitLines) {
		if (typeof splitLines == 'undefined' || splitLines) {
			tooltipContent = tooltipContent.lineSplit(48, '<br />');
		}
		this.tooltip({
			bodyHandler: function() {
				return tooltipContent;
			},
			track: true,
			delay: 0,
			showBody: " - ",
			opacity: 0.90,
			showURL: false
		});
		return this;
	}
});

/* -----------[ myPopup ]-----------------------------*/
(function($) { // Compliant with jquery.noConflict()
$.fn.myPopup = function(s) {

	s = $.extend({
		'bindClickToggle' : true,
		'onFirstShow' : null,
		'onShow' : null,
		'mouseOverElem': false,
		'openTimeout':0
	}, s || {});

	return this.each(function() {

		var self = this;

		var selfXPathSelector = $(self).XPathSelector();
		
		s.selfXPathSelector = selfXPathSelector;

		var showedOnce = false;

		function showPopup() {
			if (!s.mouseOverElem) {
				return false;
			}
			
			for (var XPathSelector in skatic.myPopup) { //close other popups
				if (XPathSelector != selfXPathSelector) {
					skatic.myPopup[XPathSelector](); //call close function
				} else {
//					console.log(XPathSelector, XPathSelector != selfXPathSelector);
				}
			}
			
			var timeout = 0;
			
			if (typeof s.onFirstShow == 'function' && !showedOnce) {
				showedOnce = true;
				s.onFirstShow(s);
				timeout += 50;
			}
			
			if (typeof s.onShow == 'function') {
				s.onShow(s);
				timeout += 10;
			}
			
			//for IE7 fix
			s.popupElem.parents('.over-el:first').css({'z-index':1000, 'border':'none'});
			
			setTimeout(function(){
				s.popupElem.show(); //.fadeIn("fast");
			}, timeout);
		}
		function hidePopup() {
			if (!s.mouseOverElem) {
				forceHidePopup();
			}
		}
		function forceHidePopup(elem) {
			if (typeof elem == 'object' || typeof elem == 'function') {
				//for IE7 fix
				elem.parents('.over-el:first').css({'z-index':999, 'border':'none'});
			
				elem.hide(); //.fadeOut("fast");
			} else {
				//for IE7 fix
				s.popupElem.parents('.over-el:first').css({'z-index':999, 'border':'none'});
				
				s.popupElem.hide(); //.fadeOut("fast");
			}
		}
		function togglePopup() {
			if (s.popupElem.is(':visible')) {
				//for IE7 fix
				s.popupElem.parents('.over-el:first').css({'z-index':999, 'border':'none'});
			
				s.popupElem.hide();
			} else {
				//for IE7 fix
				s.popupElem.parents('.over-el:first').css({'z-index':1000, 'border':'none'});
				
				showedOnce = true;
				
				s.popupElem.show();
			}
		}
		function addHover() {
			if (s.bindClickToggle) {
				$(self).bind('click', function(){
					togglePopup();
				});
			}

			var popupElemXPathSelector = s.popupElem.XPathSelector();
			
			if ($(self).has(popupElemXPathSelector).length > 0) {
				//bind only self
				var bind = self;
			} else {
				//bind self and popup element
				var bind = s.selfXPathSelector+', '+popupElemXPathSelector;
			}
			
			$(bind).bind('mouseenter', function(){
				s.mouseOverElem = true;
				setTimeout(function(){showPopup();}, s.openTimeout);
			}).bind('mouseleave',function(){
				s.mouseOverElem = false;
				setTimeout(function(){hidePopup();}, s.timeout);
			});
		}

		//add close function to container -- call when showing other popup
		if (typeof skatic.myPopup != 'object') {
			skatic.myPopup = {};
		}
		skatic.myPopup[selfXPathSelector] = function(){forceHidePopup(s.popupElem);};

		setTimeout(function(){
			addHover();
		}, 200)
	});
};
})(jQuery);

skatic.isProfilePage = function(href) {
	var inRoutes = [
		'/',
		'/login/',
		'/logout/',
		'/captcha/',
		'/support/',
		'/pages/',
		'/albums/',
		'/bans/',
		'/bookmarks/',
		'/cc_licenses/',
		'/charts/',
		'/lists/',
		'/comments/',
		'/contacts/',
		'/dropbox/',
		'/equipment/',
		'/event_types/',
		'/events/',
		'/gigs/',
		'/followers/',
		'/feeds/',
		'/genres/',
		'/invites/',
		'/locations/',
		'/mailtemplates/',
		'/menus/',
		'/message_folders/',
		'/messages/',
		'/pages/',
		'/payments/',
		'/photos/',
		'/playlists/',
		'/profiles/',
		'/recommends/',
		'/registration/',
		'/resizes/',
		'/searches/',
		'/songs/',
		'/systems/',
		'/users/',
		'/forum/',
		'/forums/',
		'/thumbups/',
		'/equipment_types/',
		'/site_pages/',
		'/video/',
		'/videos/'
	];
	
	href = skatic.url.split(href)[0];
	
	for (var i = 0; i < inRoutes.length; i++) {
		var oneRoute = inRoutes[i].replace(/\//g, '');
		
		if (oneRoute == href) {
			return false;
		}
	}
	
	//not found
	return true;
};





skatic.menu = {
	'profile':{},
	'main':{},
	'editPlaylists':{
		'mouseOverElem': false,
		'showedOnce': false,
		'loadedFor': {
			'content_type': null,
			'parent_id': null
		},
		'ctypes':{
			'audio':'Song',
			'video':'Video'
		}
	}
};

/* ---------------------------[ skatic.menu.editPlaylists ]--------------------------------------------------------- */


skatic.menu.editPlaylists.over = function(){
//	console.log('over');
	
	if (!skatic.menu.editPlaylists.mouseOverElem) {
		return false;
	}

	if (!skatic.user.isLoggedIn()) {
		skatic.flash([_('Please sign up or register to manage playlists.'), 'error']);
		return false;
	}
	
	if (skatic.menu.editPlaylists.showedOnce) {
//		console.log(skatic.menu.editPlaylists.loadedFor.content_type, skatic.player.currentPlayed.content_type, skatic.menu.editPlaylists.loadedFor.parent_id, skatic.player.currentPlayed.id);
		
		if (
			(skatic.menu.editPlaylists.loadedFor.content_type == skatic.player.currentPlayed.content_type 
				|| skatic.menu.editPlaylists.loadedFor.content_type == skatic.menu.editPlaylists.ctypes[skatic.player.currentPlayed.content_type]) 
			&& skatic.menu.editPlaylists.loadedFor.parent_id == skatic.player.currentPlayed.id) {
				
			var elem = $('#playlists .playlists');
			
			//for IE7 fix
			elem.parents('.over-el:first').css({'z-index':1000, 'border':'none'});
			
			elem.show();
		} else {
			$('#playlists .playlists').find('input').removeAttr('checked');
			$('#playlists .playlists').show();
			
			skatic.menu.editPlaylists.ajaxLoadMenu(function(){setTimeout(function(){
				var elem = $('#playlists .playlists');

				//for IE7 fix
				elem.parents('.over-el:first').css({'z-index':1000, 'border':'none'});

				elem.show();
			}, 100);});
		}
	} else {
		skatic.menu.editPlaylists.showedOnce = true;
		skatic.menu.editPlaylists.ajaxLoadMenu(function(){
			setTimeout(function(){

				$('#playlists .playlists').bind('mouseenter', function(){
//					console.info('enter');
					skatic.menu.editPlaylists.mouseOverElem = true;
					setTimeout(function(){
						skatic.menu.editPlaylists.over();
					}, 200);
				}).bind('mouseleave',function(){
//					console.info('leave');
					skatic.menu.editPlaylists.mouseOverElem = false;
					setTimeout(function(){
						skatic.menu.editPlaylists.out();
					}, 1000);
				}).show();

			}, 200);
		});
	}
};
skatic.menu.editPlaylists.out = function() {
//	console.log('out');
	
	if (!skatic.menu.editPlaylists.mouseOverElem) {
		var elem = $('#playlists .playlists');

		//for IE7 fix
		elem.parents('.over-el:first').css({'z-index':15, 'border':'none'});

		elem.hide();
	}
};
skatic.menu.editPlaylists.ajaxLoadMenu = function(onReadyCallback) {
	if (self.parent.skatic.player.currentPlayed.content_type == null) {
		var contentType = null;
	} else if (self.parent.skatic.player.currentPlayed.content_type == 'audio') {
		var contentType = 'Song';
	} else if (self.parent.skatic.player.currentPlayed.content_type == 'video') {
		var contentType = 'Video';
	}
	//can be executed from lightbox
	$.ajax({
		'url':'/playlists/playerMenu/'+(contentType != null ? contentType+'/' : '')+(self.parent.skatic.player.currentPlayed.id != null ? self.parent.skatic.player.currentPlayed.id+'/' : '')+'elementFor:player/?locationChanged=false',
		'type':'GET',
		'cache':false,
		'dataType':'html',
		'success':function(response) {
			if (response.isJSON()) {
				skatic.parseJsonResponse(JSON.parse(response));
			} else {
				$(self.parent.document).find('#playlists').html(response);

				if (typeof onReadyCallback != 'undefined') {
					onReadyCallback();
				}
			}
		}
	});
}
skatic.menu.editPlaylists.addOrRemoveFromPlaylist = function(checkbox) {
	var checked = $(checkbox).is(':checked');
	var playlistId = checkbox.id.split('_')[2];
	var contentType = checkbox.id.split('_')[3];
	var songId = checkbox.id.split('_')[4];
	
	/**
	 * pay attention to arguments order
	 * @todo standardize arguments
	 */
	if (checked) {
		var url = '/playlists/addParentRecord/'+playlistId+'/'+contentType+'/'+songId+'/';
	} else {
		var url = '/playlists/removeParentRecord/'+playlistId+'/'+contentType+'/'+songId+'/';
	}

	$.ajax({
		'type':'GET',
		'url':url+'?locationChanged=false',
		'cache':false,
		'success':function(response) {
			var checkboxes = $('input[id^="checkbox_playlist_'+playlistId+'_'+contentType+'_'+songId+'"]');
			if (checked) {
				checkboxes.attr('checked', 'checked');
			} else {
				checkboxes.removeAttr('checked');
			}
		},
		'dataType':'json'
	});
};

/*------------------------[ skatic.menu.editPlaylists.inContent ]--------------------------*/


skatic.menu.editPlaylists.inContent = {
	'mouseOverElem': {}
};

skatic.menu.editPlaylists.inContent.over = function(contentType, parentId, positioning) {
	
	//console.log('skatic.menu.editPlaylists.inContent.over', songId);
	
	if (!skatic.user.isLoggedIn()) {
		return true;
	}
	
	if ($('#wrapper .playlists_for_'+contentType+'_'+parentId).length > 0) {
		if (skatic.menu.editPlaylists.inContent.mouseOverElem[contentType+'_'+parentId]) {
			var elem = $('#wrapper .playlists_for_'+contentType+'_'+parentId);
			
			//for IE7 fix
			elem.parents('.over-el:first').css({'z-index':1000, 'border':'none'});
			
			elem.show();
		}
		return true;
	}
	
	skatic.menu.editPlaylists.inContent.ajaxLoadMenu(contentType, parentId, function(){
		setTimeout(function(){
			
			$('#wrapper .playlists_for_'+contentType+'_'+parentId).bind('mouseenter', function(){
				var contentType = this.id.split('_')[2];
				var parentId = this.id.split('_')[3];
				//console.info('mouseenter', this, songId);
				skatic.menu.editPlaylists.inContent.mouseOverElem[contentType+'_'+parentId] = true;
				setTimeout(function(){
					skatic.menu.editPlaylists.inContent.over(contentType, parentId, positioning);
				}, 200);
			}).bind('mouseleave', function(){
				//console.info('mouseleave', this, songId);
				var contentType = this.id.split('_')[2];
				var parentId = this.id.split('_')[3];
				skatic.menu.editPlaylists.inContent.mouseOverElem[contentType+'_'+parentId] = false;
				setTimeout(function(){
					skatic.menu.editPlaylists.inContent.out(contentType, parentId);
				}, 1000);
			}).show();
		
		}, 200);
	}, positioning);
};

skatic.menu.editPlaylists.inContent.out = function(contentType, parentId) {
	//console.log('skatic.menu.editPlaylists.inContent.out', songId);
//	console.info('out', contentType, parentId);
	
	if (!skatic.menu.editPlaylists.inContent.mouseOverElem[contentType+'_'+parentId]) {
		var elem = $('#wrapper .playlists_for_'+contentType+'_'+parentId);
		
		//for IE7 fix
		elem.parents('.over-el:first').css({'z-index':999, 'border':'none'});
		
		elem.hide();
	}
};

skatic.menu.editPlaylists.inContent.ajaxLoadMenu = function(contentType, parentId, onReadyCallback, positioning) {
//	console.info(contentType, parentId);
	$.ajax({
		'url':'/playlists/playerMenu/'+contentType+'/'+parentId+'/elementFor:content/?locationChanged=false',
		'type':'GET',
		'cache':false,
		'dataType':'html',
		'success':function(response) {
			var elem = $('#wrapper .addtolist[rel^="'+contentType+'_'+parentId+'"]');
			//console.log(elem);
			
			//for IE7 fix
			elem.parents('.over-el:first').css({'z-index':1000, 'border':'none'});
			
			if (positioning) {
				$('.box-inner').prepend(response);

				$(function(){
					skatic.menu.editPlaylists.inContent.positioning(elem);
				});
				
			} else {
				elem.after(response);
			}

			if (typeof onReadyCallback != 'undefined') {
				onReadyCallback();
			}
			
		}
	});
};

skatic.menu.editPlaylists.inContent.positioning = function(link) {
	var contentType = $(link).attr('rel').split('_')[1];
	var parentId = $(link).attr('rel').split('_')[2];
	//console.info($('#wrapper .playlists_for_'+contentType+'_'+parentId));
	$('#wrapper .playlists_for_'+contentType+'_'+parentId).position({
		'my': 'right top',
		'at': 'left bottom',
		'of': link,
		'offset': '60 0',
		'classAddOnCollision': 'flipped'
	});
};

$(function(){
	setTimeout(function(){
		$('.addtolist').live('mouseenter', function(event){
			
			var info = $(this).attr('rel').split('_');
			var contentType = info[0];
			var parentId = info[1];
			var positioning = typeof info[2] != 'undefined' && info[2] == 'positioning';
			
			skatic.menu.editPlaylists.inContent.mouseOverElem[contentType+'_'+parentId] = true;
			setTimeout(function(){
				skatic.menu.editPlaylists.inContent.over(contentType, parentId, positioning);
			}, 200);
			
		}).live('mouseleave', function(event){
			
			var info = $(this).attr('rel').split('_');
			var contentType = info[0];
			var parentId = info[1];
			var positioning = typeof info[2] != 'undefined' && info[2] == 'positioning';
			
			skatic.menu.editPlaylists.inContent.mouseOverElem[contentType+'_'+parentId] = false;
			setTimeout(function(){
				skatic.menu.editPlaylists.inContent.out(contentType, parentId);
			}, 1000);
			
		});
	}, 500);
});



/*--------------------------------[ skatic.menu.profile ]--------------------------*/


skatic.menu.profile = {
	'open': function() {
		$('#profilemenu').addClass('opened');
		$('#profilemenu_expanded').animate({
			'left':'0px'
		});
		//$('#profilemenu').animate({
			//'left':'360px'
		//});
	},
	'close': function() {
		$('#profilemenu').removeClass('opened');
		$('#profilemenu_expanded').animate({
			'left':'-360px'
		});
		//$('#profilemenu').animate({
			//'left':'0px'
		//});
	},
	'toggle': function(){
		//to open
		if (!$('#profilemenu').hasClass('opened')) {
			skatic.menu.profile.open();
		//close
		} else {
			skatic.menu.profile.close();
		}
	}
};

skatic.profilemenu = skatic.menu.profile; //stub

skatic.menu.main = {
	'highlighted': null,
	'highlight': function(href) {
		var menuUrls = [
			'/',
			'/songs/',
			'/profiles/',
			'/gigs/',
			'/lists/',
			'/forums/'
		];
		
		href = skatic.url.split(href)[0];
		
		var highlight = null;
		
		for (var i = 0; i < menuUrls.length; i++) {
			var oneMenuUrl = menuUrls[i].replace(/\//g, '');
			
			if (oneMenuUrl == href) {
				highlight = oneMenuUrl;
				break;
			}
		}
		
		if (highlight != null) {
			highlight = ('/'+highlight+'/').replace(/[\/]{2}/g, '/'); //remove double slashes
			$('#nav a').each(function(){
				if (skatic.url.getUrlFromHref($(this).attr('href')) == highlight) {
					$(this).addClass('on');
					skatic.menu.main.highlighted = highlight;
				} else {
					$(this).removeClass('on');
				}
			});
		} else if (skatic.menu.main.highlighted != null) {
			$('#nav a').removeClass('on');
			skatic.menu.main.highlighted = null;
		}
	}
};


