$(document).ready(function(){

	$('h1').click(function(){
		alert(returnScroll())
	})

	$("a.sideplay").click(function(){	
		//alert( $("body").scrollTop() );
		$('#player').empty();
		$('#playerbox').css('top',  returnScroll() );	
		$('#overlay div#player').load($(this).attr('href'), $('#overlay').show());
		
		return false;			
	})
		
	$("#sidebar h3 a").click(function(){
		$('#sidebar .sidebar_content').hide();
		//alert($(this).parent().parent().find(".sidebar_content").css('display'))
		if($(this).parent().parent().find(".sidebar_content").css('display')=='none'){
			$(this).parent().parent().find(".sidebar_content").fadeIn();
		}
		return false
	})
	
	
	$('a.x').click(function(){
		$('#overlay').hide();
		$('#overlay #playerbox #player').empty();
		return false;
	})
		
	$('a.play').click(function(){
		$('#player').empty();
		$('#playerbox').css('top',  returnScroll() );
		var id = $(this).attr('rel');
		$('#overlay div#player').load('/?s=embed&id='+id,$('#overlay').fadeIn());
		return false;
	})

	subscription();	
	
});

function returnScroll(){
	return 50 + Math.max($("html").scrollTop(), $("body").scrollTop());
}

function subscription(){

			var def = "your@email"	;
			$('#e').focus(function(){
				var val = $('#e').val();
				$('#e').val(inputrewrite(val,def));
			});
			$('#e').blur(function(){
				var val = $('#e').val();
				$('#e').val(inputrewrite(val,def));
			});

			$('#e_submit').click(function(){
				var val = $('#e').val();
				var container = $('#e').parent();
				if(!echeck(val))return false;
				$('#e').fadeOut('slow');
				container.empty().append("Please Wait...");			
				container.load("/subscribe.php?e="+val);
			})	
		}

		function inputrewrite(v,d){	 
			if (v==d) {
				return "";
			}else if(v==""){
				return d;
			}
		}



		function echeck(str) {

							var at="@"
							var dot="."
							var lat=str.indexOf(at)
							var lstr=str.length
							var ldot=str.indexOf(dot)
							if (str.indexOf(at)==-1){
							   alert("Invalid E-mail")
							   return false
							}

							if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
							   alert("Invalid E-mail")
							   return false
							}

							if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
							    alert("Invalid E-mail")
							    return false
							}

							 if (str.indexOf(at,(lat+1))!=-1){
							    alert("Invalid E-mail")
							    return false
							 }

							 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
							    alert("Invalid E-mail")
							    return false
							 }

							 if (str.indexOf(dot,(lat+2))==-1){
							    alert("Invalid E-mail")
							    return false
							 }

							 if (str.indexOf(" ")!=-1){
							    alert("Invalid E-mail")
							    return false
							 }

					 		 return true					
						}



// $(document).ready(function(){
// 	if(!hasCSS()){return false;}
//  
// 
// 	
// 	$(window).unload(function(){ 
// 		if(state){
// 			$(".icon, .window").each(function(){
// 				updateState($(this));
// 			})
// 		saveState();
// 			
// 		}
// 		}
// 	);
// 
// 	$('.window').draggable({stop: function(event, ui){ updateState($(this))}, handle:'div.handle', cursor:"move" ,stack:{ group: $('.window'), min:1000}});	
// 	
// 	$('.icon:not(.inactive)').draggable({stop: function(event, ui){ updateState($(this))},start: function(event, ui){ highlight($(this))},stack:{ group: $('.icon'), min:100}})
// 	// $('.icon').click(function(){
// 	// 	
// 	// })
// 	$(window).resize(function(){
// 	  layout();
// 	});
// 		
// 	$('.window').resizable({minWidth:780, minHeight:580, handles:'se'});
// 	$('.window').hide();
// 	
// 
// 	getState();
// 	renderState();
// 	layout();
// 	subscription();
// 
// 	$('ul#menu li.text a').click(function(){
// 		//closeAllWindows();
// 		//$('ul#menu li a.section-title.highlight').removeClass('highlight');
// 		//$('.icon').draggable('enable').removeClass('inactive');
// 		var id = '.window#win_' + ($(this).attr('rel')).toString();		
// 		launchWindow($(id));
// 		//$(this).addClass('highlight');
// 		return false;
// 		
// 		
// 	})
// 	
// 	$('ul#menu li.proj a').click(function(){
// 		if($(this).parent().hasClass('highlight')){
// 			$(this).parent().removeClass('highlight');
// 			$('.icon.inactive .screen').remove();
// 			$('.icon').draggable('enable').removeClass('inactive active highlight');
// 			return false;
// 		}
// 		
// 		closeAllWindows();
// 		
// 		var tag = 'tag_'+($(this).attr('title')).toString();
// 		$('.icon:not(.'+tag+')').addClass('inactive').draggable('disable').append('<div class="screen"></div>');
// 		
// 		$('.icon.'+tag).removeClass('inactive highlight').addClass('active').draggable('enable').each(function(){
// 			$(this).css('zIndex', maxZ($('.icons'))+1);
// 			$(this).children('div.screen').remove();
// 		});
// 		
// 		$('ul#menu li.highlight').removeClass('highlight');
// 		$(this).parent().addClass('highlight');
// 		return false;
// 
// 	})
// 	
// 	$(".window a.x").click(function(){
// 		closeWindow($(this).parent());
// 		return false;
// 	})
// 	
// 		
// 	$(".icon").click(function(){
// 		if($(this).hasClass('inactive')) return false;
// 		var id = '.window#win_' + ($(this).attr('rel')).toString();
// 		highlight($(this));	
// 		launchWindow($(id));	
// 		return false;
// 	});
// 	
// 
// 	$('div.exhibit ul.media').jqGalScroll({width:480,height:460,speed:500,titleOpacity:1});
// 
// })
// 
// 
// function windowSize(){
// 	
// 	return {w:$(window).width(), h:$(window).height()}
// }
// 
// 
// function newWindow(){
// 	
// 	var bounds = windowSize()
// 	var new_w = Math.min( 880  ,Math.max(bounds.w*.77, 700));
// 	var new_h = Math.max(bounds.h*.8, 580);
// 
// 
// 	var new_x = (bounds.w - new_w) /2;
// 	var new_y = Math.max((bounds.h-new_h)/4,0);
// 	
// 	
// 	
// 	while( !isPosAvail(new_x,new_y) ){		
// 		new_x = new_x +15;
// 		new_y =+ new_y + 15;		
// 	}
// 		
// 	
// 	
// 	return {x: new_x  ,y: new_y , w:new_w, h:new_h}
// }
// 
// 
// function isPosAvail(x,y){
// 	
// 	var flag = false;
// 	$('.window.open').each(function(){
// 		var p = $(this).position();
// 		
// 		if( Math.floor(x) == p.left || y== p.top) {flag=true;}		
// 		})
//     
//      if(flag==true) return false;
// 
//      return true;
// 	
// }
// 
// function getState(){
// 	
//   
// 	if (getCookie('_state')){
// 		state = eval(getCookie('_state'));					
// 	}else{
// 		state = eval(state);
// 	}
// 
// 
// 		
// }
// 
// 
// 
// 
// function distributeIcons(){
// 	
// 	var tilesize =175;
// 	var field = windowSize() ;
// 	var xtiles = Math.floor(field.w/tilesize) ;
// 	var ytiles = Math.floor(field.h/tilesize) ;
// 	var map = [];
//     
// 	for(var i = 0; i < xtiles*ytiles;i++ ){
// 		map.push(i);
// 	}		
// 	//alert('map lenght: '+map.length)		
// 	$('.icon').each(function(){
// 		var tile = map.splice(Math.floor ( Math.random()*map.length ), 1 );
// 		var tiley = tilesize * ( Math.floor(tile/xtiles) );
// 		var tilex = tilesize * ( tile % xtiles ) ;
// 		$(this).css({'top':tiley,'left':tilex});
// 	
// 	}) 	
// }
// 
// 
// function renderState(){	
// 	
// 	if(window.location.hash){
// 		var urlid = '#win_'+window.location.hash.replace("#/","");
// 		
// 		$('.window').each(function(){
// 		    removeFromState($(this));
// 		})
// 
// 		launchWindow($(urlid));
// 	}
// 
// 
// 
// 	for (var i = state.length - 1; i >= 0; i--){
// 		
// 		if(state[i]._id=='' || state[i]._id==null ) continue;
// 		var _o = $('#'+state[i]._id);
// 
// 	    _o.css('top',state[i]._y);
// 		_o.css('left',state[i]._x);
// 		_o.css('zIndex', state[i]._z);
//  		
// 	 	if(state[i]._class=='window') {
// 	 		_o.width(state[i]._w);
// 	 		_o.height(state[i]._h);
// 		    _o.show().addClass('open');
// 	    }			 	
// 	}	
// }
// 
// 
// function layout(){
// 	
// 	var winSize = windowSize();
// 	
// 	$("body").height(winSize.h);	
// 	$("#desktop").height(winSize.h-32);
// 	$('#desktop').width(winSize.w-12);
// 	$('.window.open, .icon').each(function(){
// 		bounds($(this));
// 	});	
// }
// 
// 
// function bounds(_o){
//     
// 	var w = _o.width();
// 	var h = _o.height();
// 	var xy= _o.position();
// 	var x = xy.left;
// 	var y = xy.top;	
// 	var bounds = windowSize();
// 		
// 	if(_o.hasClass('.icon')){
// 		if(x+100>bounds.w)_o.css('left',bounds.w-w-50)
// 		if(x<12) _o.css('left',0);
// 
// 		if(y+h>bounds.h) _o.css('top',bounds.h-h-50); 
// 		if(y<0) _o.css('top', 0);
// 		return;
// 	}
// 	
// 	if(x+50>bounds.w) {_o.css('left',bounds.w-100)}
// 	if(x+w<100) {_o.css('left',100	-w);}
// 	
// 	if(y>bounds.h-100){ _o.css('top',bounds.h-100) }
// 	if(y<0) {_o.css('top', 0);}
// 	
// }
// 			
// function updateState(_o){
// 	
//  bounds(_o);
//  var p = _o.position();
//  var w = 0;
//  var h = 0;
//  var c = 'icon';
// 
//  if(_o.hasClass('window')){
// 	w = _o.width();
// 	h = _o.height();
// 	c = 'window';
// 	
// 	if(!_o.hasClass('open')) return 
// 	
//  }
//  
//  var item = {_id:_o.attr("id"), _x:p.left, _y:p.top, _z:_o.css('z-index'), _w:w, _h:h, _class:c }
// 
// 
// 
//  for (var i=0; i < state.length; i++) {
// 		if(item._id==state[i]._id){
// 			state.splice(i,1);
// 		}			
// 	}
//   	
//   state.push(item);
// 
//   return;
//  
// }
// 
// function removeFromState(_o){
// 	//
// 	// grab the id of the closing window, match it to a position in the state array and remove it
// 	//
// 	
//     var window_id = _o.attr('id');
// 	for (var i=0; i < state.length; i++) {
// 		if(window_id==state[i]._id){
// 			state.splice(i,1);
// 			return;
// 		}			
// 	}
// }
// 
// function saveState(){
// 	var newstate = JSON.stringify(state);
// 	setCookie("_state", newstate, 90);
// }
// 
// 
// function maxZ(_o){
// 	var maxz = Math.max.apply(Math, $.map(_o,function(e,n){   
// 		 return parseInt($(e).css('zIndex'))||1000
// 		}))
// 	return maxz;
// }
// 
// 
// 
// 
// 
// 
// function closeAllWindows(){
// 	
// 	$('.window.open').each(function(){
// 		closeWindow($(this));
// 	})
// }
// 
// 
// function closeWindow(_o){
// 	_o.hide().removeClass('open');
// 	removeFromState(_o);
// }
// 
// function launchWindow(_w){	
// 
// 	if( _w.css('display')!='none') {_w.css('zIndex',maxZ($('.window'))+1);; return;}
// 	var w = newWindow();
// 	_w.css('top',w.y);
// 	_w.css('left',w.x);
// 	_w.css('zIndex',maxZ($('.window'))+1);
// 	_w.height(w.h);
// 	_w.width(w.w);	
// 	_w.show().addClass('open');
// 	_w.children('div.exhibit').scrollTop(0);
// 	updateState(_w,'window');
// 	//window.location.hash = "/"+_w.attr('id').substring(4,_w.attr('id').length);
// }
// 
// 
// function highlight(_o){
// 	$('.icon.highlight').removeClass('highlight');
// 	_o.addClass('highlight');
// }
// 
// 
// 
// 
// 
// 
// function sortIcons(){
// 	
// }
// 
// 
// 
// 
// 
// function getCookie(c_name)
// {
// 	if (document.cookie.length > 0)
// 	{
// 		c_start = document.cookie.indexOf(c_name + "=")
// 
// 		if (c_start != -1)
// 		{ 
// 			c_start = c_start + c_name.length + 1 
// 			c_end = document.cookie.indexOf(";", c_start)
// 			
// 			if (c_end == -1) c_end = document.cookie.length
// 			return unescape(document.cookie.substring(c_start, c_end))
// 		} 
// 	}
// 	return 0;
// }
// 
// function setCookie(c_name,value,expiredays)
// {
// 	var exdate = new Date();
// 	exdate.setDate(exdate.getDate() + expiredays);
// 	document.cookie = c_name + "=" + escape(value) +
// 	((expiredays==null) ? "" : ";expires=" + exdate.toGMTString()) + '; path=/';
// }
// 
// 
// 
// 
// function subscription(){
// 			var def = "your@email"	;
// 			$('#e').focus(function(){
// 				var val = $('#email').val();
// 				$('#ee').val(inputrewrite(val,def));
// 			});
// 			$('#e').blur(function(){
// 				var val = $('#e').val();
// 				$('#e').val(inputrewrite(val,def));
// 			});
// 
// 			$('#e_submit').click(function(){
// 				var val = $('#e').val();
// 				var container = $('#e').parent();
// 				if(!echeck(val))return false;
// 				$('#e').fadeOut('slow');
// 				container.empty().append("Please Wait...");			
// 				container.load("/subscribe.php?e="+val);
// 			})	
// 		}
// 
// 
// 
// 		function echeck(str) {
// 
// 							var at="@"
// 							var dot="."
// 							var lat=str.indexOf(at)
// 							var lstr=str.length
// 							var ldot=str.indexOf(dot)
// 							if (str.indexOf(at)==-1){
// 							   alert("Invalid E-mail")
// 							   return false
// 							}
// 
// 							if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
// 							   alert("Invalid E-mail")
// 							   return false
// 							}
// 
// 							if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
// 							    alert("Invalid E-mail")
// 							    return false
// 							}
// 
// 							 if (str.indexOf(at,(lat+1))!=-1){
// 							    alert("Invalid E-mail")
// 							    return false
// 							 }
// 
// 							 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
// 							    alert("Invalid E-mail")
// 							    return false
// 							 }
// 
// 							 if (str.indexOf(dot,(lat+2))==-1){
// 							    alert("Invalid E-mail")
// 							    return false
// 							 }
// 
// 							 if (str.indexOf(" ")!=-1){
// 							    alert("Invalid E-mail")
// 							    return false
// 							 }
// 
// 					 		 return true					
// 						}
// 
// 
// // 
// 
// 
// 						function hasCSS(){
// 							$('body').append($(document.createElement('div')).attr('id','css_test').css({width:'1px',height:'1px',display:'none'}));
// 							var _v=($('#css_test').width()!=1)?false:true;
// 							$('#css_test').remove();return _v;
// 						};
// 
// 
// 						function parseUrl(){;
// 							var id =  window.location.hash.replace("#/","");					
// 							launchWindow($('.window#win_'+id));
// 						}