var WEBSITE = jQuery('script')[0].src.match('(.*)/workspace')[1];
var member_id;

(function($){
	$(window).load(function(){
		member_id = $('#member-info ul').attr('id');
		/* News */
		$('#news').liquidCanvas("shadow => roundedRect");
		
		function refreshNews(){
			$('#news .prev:not(.disabled)').liquidCanvas("[fill{color:#cddbe5} image{url:"+WEBSITE+"/workspace/images/arrow-prev-news.png;vertical:middle;horizontal:center}] => customRect{topLeft:5;bottomLeft:5}");
			$('#news .prev.disabled').liquidCanvas("[fill{color:#cddbe5} image{url:"+WEBSITE+"/workspace/images/arrow-prev-disabled-news.png;vertical:middle;horizontal:center}] => customRect{topLeft:5;bottomLeft:5}");

			$('#news .next:not(.disabled)').liquidCanvas("[fill{color:#cddbe5} image{url:"+WEBSITE+"/workspace/images/arrow-next-news.png;vertical:middle;horizontal:center}]=> customRect{topRight:5;bottomRight:5}");
			$('#news .next.disabled').liquidCanvas("[fill{color:#cddbe5} image{url:"+WEBSITE+"/workspace/images/arrow-next-disabled-news.png;vertical:middle;horizontal:center}]=> customRect{topRight:5;bottomRight:5}");
		}
		
		refreshNews();
		
		$('#news .prev:not(.disabled)').live('mouseenter', function(){
			$(this).liquidCanvas("[fill{color:#71808b} image{url:"+WEBSITE+"/workspace/images/arrow-prev-news.png;vertical:middle;horizontal:center}] => customRect{topLeft:5;bottomLeft:5}");
		});
		$('#news .prev:not(.disabled)').live('mouseleave', function(){
			$(this).liquidCanvas("[fill{color:#cddbe5} image{url:"+WEBSITE+"/workspace/images/arrow-prev-news.png;vertical:middle;horizontal:center}] => customRect{topLeft:5;bottomLeft:5}");
		});
		$('#news .prev.disabled').live('mouseleave', function(){
			$(this).liquidCanvas("[fill{color:#cddbe5} image{url:"+WEBSITE+"/workspace/images/arrow-prev-disabled-news.png;vertical:middle;horizontal:center}] => customRect{topLeft:5;bottomLeft:5}");
		});
		$('#news .next:not(.disabled)').live('mouseenter', function(){
			$(this).liquidCanvas("[fill{color:#71808b} image{url:"+WEBSITE+"/workspace/images/arrow-next-news.png;vertical:middle;horizontal:center}] => customRect{topRight:5;bottomRight:5}");
		});
		$('#news .next:not(.disabled)').live('mouseleave', function(){
			$(this).liquidCanvas("[fill{color:#cddbe5} image{url:"+WEBSITE+"/workspace/images/arrow-next-news.png;vertical:middle;horizontal:center}] => customRect{topRight:5;bottomRight:5}");
		});
		$('#news .next.disabled').live('mouseleave', function(){
			$(this).liquidCanvas("[fill{color:#cddbe5} image{url:"+WEBSITE+"/workspace/images/arrow-next-disabled-news.png;vertical:middle;horizontal:center}] => customRect{topRight:5;bottomRight:5}");
		});
		
		$('#news .prev').click(refreshNews);
		$('#news .next').click(refreshNews);
		
		$('#news .blue').liquidCanvas("gradient{from:#e4eef6; to:#cadae6} => rect");
		
		/* Exclusives */
		$('#exclusives').liquidCanvas("shadow => roundedRect");
		$('#exclusives .prev, #exclusives .next').height($('#exclusives .container').outerHeight());
		$('#exclusives .prev:not(.disabled)').liquidCanvas("[fill{color:#d0d0d0} image{url:"+WEBSITE+"/workspace/images/arrow-prev-news.png;vertical:middle;horizontal:center}] => customRect{topLeft:5;bottomLeft:5}");
		$('#exclusives .prev.disabled').liquidCanvas("fill{color:#E3E3E3} => customRect{topLeft:5;bottomLeft:5}");

		$('#exclusives .next:not(.disabled)').liquidCanvas("[fill{color:#d0d0d0} image{url:"+WEBSITE+"/workspace/images/arrow-next-news.png;vertical:middle;horizontal:center}]=> customRect{topRight:5;bottomRight:5}");
		$('#exclusives .next.disabled').liquidCanvas("fill{color:#E3E3E3} => customRect{topRight:5;bottomRight:5}");
		
		$('#footer').liquidCanvas("fill{color:#000} => customRect{topLeft:5;topRight:5}");
		$('.box.grey, .ad-box.grey').liquidCanvas("[shadow gradient{from:#f2f2f2; to:#e0e0e0}] => roundedRect, fill{color:#fff} => headline");
		$('.box.blue, .ad-box.blue').liquidCanvas("[shadow gradient{from:#e4eef6; to:#cadae6}] => roundedRect, fill{color:#fff} => headline");
		$('.box.black, .ad-box.black').liquidCanvas("[shadow fill{color:#000;}] => roundedRect, fill{color:#fff} => headline");
		
		$('.home-box.grey').liquidCanvas("[shadow gradient{from:#f2f2f2; to:#e0e0e0}] => roundedRect, fill{color:#fff} => headline{top:44}");
		$('.home-box.blue').liquidCanvas("[shadow gradient{from:#e4eef6; to:#cadae6}] => roundedRect");
		$('.home-box.black').liquidCanvas("[shadow fill{color:#000}] => roundedRect, fill{color:#fff} => headline{top:44}");
		$('.popup .box').liquidCanvas("[shadow gradient{from:#f2f2f2; to:#e0e0e0}] => roundedRect, fill{color:#fff} => headline");
		$('.tooltip.top').liquidCanvas("[shadow gradient{from:#f2f2f2; to:#e0e0e0}] => tooltip");
		$('.tooltip.bottom').liquidCanvas("[shadow btnGradient{from:#909090; to:#000}] => tooltip{position:bottom}");
		$('.addthis_toolbox').liquidCanvas("[shadow gradient{from:#f2f2f2; to:#e0e0e0}] => roundedRect");
		
		$('#newsletter input[type=submit]').liquidCanvas("btnGradient{fromColor:#dfedf7; toColor:#b1c6d5} => customRect{topRight:5;bottomRight:5}]");
		/* Buttons */
		$('.button').liquidCanvas("btnGradient => roundedRect{radius:5}");
		$('.pbutton').liquidCanvas("btnGradient{fromColor:#a6b5c0; toColor:#71808b} => roundedRect");
		$('.mbutton').liquidCanvas("btnGradient{fromColor:#a6c6f2; toColor:#5587cb} => roundedRect");
		$('.abutton').liquidCanvas("btnGradient{fromColor:#ff8888; toColor:#da1515} => roundedRect");
		
		$('.button').hover(function(){
			$(this).liquidCanvas("btnGradient => roundedRect{radius:5}");
		}, function(){
			$(this).liquidCanvas("btnGradient{toPercent:36} => roundedRect");
		});
		
		$('.mbutton').hover(function(){
			$(this).liquidCanvas("btnGradient{fromColor:#a6c6f2; toColor:#5587cb; toPrercent: 36} => roundedRect");
		}, function(){
			$(this).liquidCanvas("btnGradient{fromColor:#a6c6f2; toColor:#5587cb} => roundedRect");
		});
		
		$('.abutton').live('mouseenter', function(){
			$(this).liquidCanvas("btnGradient{fromColor:#ff8888; toColor:#da1515; toPercent:36} => roundedRect");
		});
		
		$('.abutton').live('mouseleave', function(){
			$(this).liquidCanvas("btnGradient{fromColor:#ff8888; toColor:#da1515} => roundedRect");
		});
		
		$('#exclusives-header, #video-player, #picture-viewer').liquidCanvas("[shadow fill{color:#000}] => roundedRect");
		
		/*$('a#logout').bind('click', function(e){
			e.preventDefault();
			$('#member-info').submit();
		});*/
		$('a#login').click(function(e){
			e.preventDefault();
			if($('#popup-login').is(':hidden')) {
				positionBox('a#login', '#popup-login');
				attachEscape('#popup-login');
			} else {
				$('#popup-login').fadeOut();
			}
		});
		$('a#invite').click(function(e){
			e.preventDefault();
			if($('#popup-invite').is(':hidden')) {
				positionBox('a#invite', '#popup-invite');
				attachEscape('#popup-invite');
			} else {
				$('#popup-invite').fadeOut();
			}
		});
		
		if($('#popup-login').is(':visible')) {
			attachEscape('#popup-login');
		}
		if($('#popup-invite').is(':visible')) {
			attachEscape('#popup-invite');
		}
		
		function positionBox(link, box){
			var w, x, y;
			if($(box).is(':hidden') == true){
				$(box).css({'top': '-2000px', 'display': 'block'}).find('.box').liquidCanvasRefresh();
				$(box).find('.button').liquidCanvasRefresh();
				w = $(box).width();
				$(box).hide();
			} else {
				w = $(box).width();
			}
			
			x = $(link).offset().left + $(link).width();
			y = $(link).offset().top + $(link).height()+12;
			
			$(box).css({'left': x-w, 'top': y});
		}
		
		function attachEscape(o) {
			$(o).fadeIn().find('input').keydown(function(e){
				if(e.keyCode == 27){
					$(o).fadeOut();
				}
			}).first().focus();
		}
		
		//$('#follow .follow-twitter').tooltip({tip: '#twitter-icons'});
		$('#top-menu a').hover(function(){
			$('#menutooltip p').text($(this).attr('title'));
			$('#menutooltip').css({'top': '-2000px', 'display': 'block'}).find('.tooltip').liquidCanvasRefresh();
			var w = $('#menutooltip').width();
			$('#menutooltip').css({'left': ($(this).position().left+$(this).width()/2-w/2), 'top': ($(this).height()+65) });
			$(this).attr('title', '');
		}, function(){
			if($('#menutooltip p').text()){
				$(this).attr('title', $('#menutooltip p').text());
			}
			$('#menutooltip').hide();
		});
		
		$('#mediapack .button').toggle(function(){
			$('#mediapacktip').css({'top': '-2000px', 'display': 'block'}).find('.tooltip').liquidCanvasRefresh();
			var w = $('#mediapacktip').width();
			var h = $('#mediapacktip').height();
			$('#mediapacktip').css({'left': ($(this).position().left+$(this).outerWidth()/2-w/2), 'top': ($(this).position().top-h) });//.hide();
//			$('#mediapacktip').show();
		}, function(){
			$('#mediapacktip').hide();
		});
		
		if($('a#login').length) positionBox('a#login', '#popup-login');
		//positionBox('a#invite', '#popup-invite');
		$('#for-you li:first').css({"font-weight": "bold"});
		$('#for-you-image .fy-img:not(:first)').css({"display": "none"});
		
		sequence(0);
		function sequence(num){
			var next = (num+1)%3;
			$('#for-you li').css({"font-weight": "normal"});
			$('#for-you li:eq('+num+')').css({"font-weight": "bold"});
			$('#for-you-image .fy-img:eq('+num+')')
				.fadeIn().delay(2000)
				.fadeOut( function(){
					sequence(next);
				});
		}
		
		
		$('#username').focusout(function(){
			var filter=new RegExp(/^([\w-]+)$/i);
			if (filter.test($(this).val())){
				$.get( WEBSITE + '/check-available-username/' + $(this).val() + '/', function(data){
					$('#username').attr('class', $(data).attr('class')).next().replaceWith(data);
				});
			} else {
				$('#username').attr('class', 'error').next().replaceWith('<span class="error">Username contains invalid characters.</span>');
			}
			if($(this).val() == ''){
				$(this).attr('class', 'error').next().replaceWith('<span class="error">This is a required field.</span>');
			}
		});
		
		$('#email-address').focusout(function(){
			var filter=new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);
			if (filter.test($(this).val())){
				$.get( WEBSITE + '/check-available-email/' + $(this).val() + '/', function(data){
					$('#email-address').attr('class', $(data).attr('class')).next().replaceWith(data);
				});
			} else {
				$('#email-address').attr('class', 'error').next().replaceWith('<span class="error">This is not a valid email address.</span>');
			}
			if($(this).val() == ''){
				$(this).attr('class', 'error').next().replaceWith('<span class="error">This is a required field.</span>');
			}
		});
		
		$('#password').focusin(function(){
			if($(this).next().attr('class') != 'check'){
				$(this).attr('class','check').next().replaceWith('<span class="check"><q/><q/><q/><q/><q/><q/><i class="level"/></i>');
			}
		}).keyup(function(){
			var strength = checkPassword($(this).val());
			$(this).next().find('q').removeAttr('class');
			$(this).next().find('q:lt('+strength+')').attr('class','point');
			var level;
			switch(strength){
				case 6: level = "very secure";break;
				case 5: level = "secure";break;
				case 4: level = "very strong";break;
				case 3: level = "strong";break;
				case 2: level = "avarage";break;
				case 1: level = "weak";break;
				case 0: level = "very weak";break;
			}
			$(this).next().find('.level').text(level);
		}).focusout(function(){
			if($(this).val() == ''){
				$(this).attr('class', 'error').next().replaceWith('<span class="error">This is a required field.</span>');
			}
		});
		
		$('#fields-name').focusout(function(){
			if($(this).val() == ''){
				$(this).attr('class', 'error').next().replaceWith('<span class="error">This is a required field.</span>');
			} else {
				$(this).attr('class', 'good').next().replaceWith('<span class="good">ok</span>');
			}
		});
		
		$('#fields-email').focusout(function(){
			var filter=new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);
			if (filter.test($(this).val())){
				$('#fields-email').attr('class', 'good').next().replaceWith('<span class="good">ok</span>');
			} else {
				$('#fields-email').attr('class', 'error').next().replaceWith('<span class="error">This is not a valid email address.</span>');
			}
			if($(this).val() == ''){
				$(this).attr('class', 'error').next().replaceWith('<span class="error">This is a required field.</span>');
			}
		});
		
		var abuse_id;
		$('.abuse').click(function(e){
			e.preventDefault();
			abuse_id = $(this).attr('id').substring(6);
			if($('#abuse').is(':hidden')){
				$('#abuse').css({'top': '-2000px', 'display': 'block'}).find('.box').liquidCanvasRefresh();
				$('#abuse .button').liquidCanvasRefresh();
				var w = $('#abuse').width();
				$('#abuse').css({'left': ($(this).position().left-w), 'top': ($(this).position().top) });//.hide();
			} else {
				$('#abuse').hide();
			}
		});
		
		$('#no').click(function(){
			$('#abuse').hide();
		});
		$('#yes').click(function(){
			jQuery.post( WEBSITE + '/abuse/', {
				'fields[comment]': abuse_id,
				'fields[member]': member_id,
				'action[abuse]': 'Send'
			}, function(data){
				$('#abuse').hide();
			});
		});
		$('.upload a').click(function(e){
			e.preventDefault();
			$(this).hide().next().show();
		});
		
		var total = $('#picture-viewer .item').length;
		if(total>0){
			
			$("#picture-viewer a.image").overlay({
				target: '#gallery', 
				expose: '#ffffff',
				left: 'center'
			}).gallery({ 
				autohide: false,
				info: '.controls',
				opacity: 1,
				template: '<a class="prev"><img src="'+WEBSITE+'/workspace/images/arrow-prev-scroll.gif"/></a><span class="counter">PHOTO ${index}/${total}</span><a class="next"><img src="'+WEBSITE+'/workspace/images/arrow-next-scroll.gif"/></a>'
			});
			
			var counter = 0;
			
			$('.controls .counter').text('PHOTO ' + (counter + 1) + '/' + total );
			$('.controls .prev').live('click', function(){
				if( counter > 0 ) {
					$('#picture-viewer .item:eq('+counter+')').fadeOut();
					counter--;
					$('#picture-viewer .item:eq('+counter+')').fadeIn();
					$('.controls .counter').text('PHOTO ' + (counter + 1) + '/' + total );
				}
			});
			$('.controls .next').live('click', function(){
				if( counter < total-1 ) {
					$('#picture-viewer .item:eq('+counter+')').fadeOut();
					counter++;
					$('#picture-viewer .item:eq('+counter+')').fadeIn();
					$('.controls .counter').text('PHOTO ' + (counter + 1) + '/' + total );
				}
			});
			$('.controls').fadeIn();
			$('#picture-viewer .item:first').fadeIn();
		}
	});
})(jQuery);

// Ajax
function favorite(state, favorite_id, id){
	jQuery.post( WEBSITE + '/favorite/', {
		'fields[favorite]': state,
		'fields[media]': favorite_id,
		'fields[member]': member_id,
		'id': id,
		'action[favorite]': 'Send'
	}, function( data ){
		jQuery('#'+favorite_id).replaceWith(data);
	});
}
function attend(state, event_id, id){
	jQuery.post( WEBSITE + '/attending/', {
		'fields[attend]': state,
		'fields[event]': event_id,
		'fields[member]': member_id,
		'id': id,
		'action[attending]': 'Send'
	}, function( data ){
		jQuery('#attend').prev().remove();
		jQuery('#attend').replaceWith(data);
		jQuery('#attend').liquidCanvas("btnGradient{fromColor:#ff8888; toColor:#da1515} => roundedRect");
	});
}

// based on Password strength meter v2.0 by Matthew R. Miller 2007 www.codeandcoffee.com
function checkPassword(pass){
	var strUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var strLowerCase = "abcdefghijklmnopqrstuvwxyz";
	var strNumber = "0123456789";
	var strCharacters = "!@#$%^&*()_-+=[{}];:'\"|\\,.<>?/~`£§";
	function countContain(pass, strCheck) { 
		var nCount = 0;
		for (var i = 0; i < pass.length; i++) {
			if (strCheck.indexOf(pass.charAt(i)) > -1){nCount++;}
		}
		return nCount; 
	}
	var nScore = 0;
	if (pass.length < 5){nScore += 5;}
	else if (pass.length >= 5 && pass.length < 8){nScore += 10;}
	else if (pass.length >= 8){nScore += 25;}
	var nUpperCount = countContain(pass, strUpperCase);
	var nLowerCount = countContain(pass, strLowerCase);
	var nLowerUpperCount = nUpperCount + nLowerCount;
	if (nLowerCount != 0){nScore += 10;}
	if (nUpperCount != 0){nScore += 10;}
	var nNumberCount = countContain(pass, strNumber);
	if (nNumberCount >= 1 && nNumberCount <3){nScore += 10;}
	else if (nNumberCount >= 3){nScore += 20;}
	var nCharacterCount = countContain(pass, strCharacters);
	if (nCharacterCount == 1){nScore += 10;}	
	else if (nCharacterCount > 1){nScore += 25;}
	if (nNumberCount != 0 && nUpperCount != 0 && nLowerCount != 0 && nCharacterCount != 0){nScore += 5;}
	else if (nNumberCount != 0 && nLowerUpperCount != 0 && nCharacterCount != 0){nScore += 3;}
	else if (nNumberCount != 0 && nLowerUpperCount != 0){nScore += 2;}
	var level = 0;
	if(nScore>=90){level=6;}
	else if(nScore>=80){level=5;}
	else if(nScore>=70){level=4;}
	else if(nScore>=60){level=3;}
	else if(nScore>=50){level=2;}
	else if(nScore>=25){level=1;}
	
	return level;
}
