var media_url = "/2012/site_media/";

// do not add the trailing slash.
var base_url = "/2012";
var i=0;
function initupdates()
{
	$("#update").fadeOut('normal',function(){
	$("#update").html(updates[i++]);
	if(i==updates.length)
		i=0;
	$("#update").fadeIn();
	setTimeout('initupdates()',5000);
	});
}
$(document).ready(
    function()
    {
  //To check for textbox in registration form  
var loc = location.pathname;
var flagblog=0;

$("#username").focus(function(){$(this).val("");});
$("#username").focusout(function(){if($(this).val()==""){$(this).val("Username");}});
$("#password").focus(function(){$(this).val("");});
$("#password").focusout(function(){if($(this).val()==""){$(this).val("CCTVROCKS");}});
$("#logindisplay").click(function(){
$("#lwrapper").fadeIn();
});

$("#loginclose").click(function(){$("#lwrapper").fadeOut();});

$("#blogs").hover(function(){$("#blogstext").slideDown();flagblog=1;},function(){if(flagblog==1){$("#blogstext").slideUp();}});
$("#blogstext").hover(function(){$(this).css({'display':'block'});},function(){$(this).slideUp();});

				if(loc.charAt(loc.length-1)=="/")
				{
					loc=loc.substring(0,loc.length-1);
				}
				var locnew=loc.split("/");
				if((locnew[locnew.length-1]=="register"||locnew[locnew.length-2]=="register"))
        {
        if(($("#id_college_on_deck > div").html()!=""))
        {
        $("#id_college_text").css({'visibility':'hidden'});
        }
        }
	//end
		initupdates();
      var start=1;
      var categorycount=0;
      var catleft=0;
      var buttonon="p1";
      var registerflag=0;
function registerevent(registerid)
{
$('#register').html('<img style="margin-top:10px;" src="'+media_url+'images/content_loader.gif" />');
	$.post('/2012/addevent/',{'id' :registerid},function(code){
									
									if (code == '1')
									$('#register').html('<p><font color="green">You have registered!</font></p>');
									else
									$('#register').html('<p><font color="red">You need to be logged in</font></p>');
									registerflag=1;
									$("#register").css({'cursor':'default'});
									});
}

      $(".cat").each(function()
        {
          categorycount++;
        }
        );
		var offsetleft=(categorycount-8)*(-150);
		
		$("#righta").click(function(){
			catleft-=150;
			$("#categories").animate({'margin-left':catleft});
			if(catleft<=offsetleft) document.getElementById('righta').style.visibility="hidden";		
			if(catleft<0) document.getElementById('lefta').style.visibility="visible";		
			
		});
		$("#lefta").click(function(){
			catleft+=150;
			$("#categories").animate({'margin-left':catleft});
			if(catleft>offsetleft) document.getElementById('righta').style.visibility="visible";		
			if(catleft==0) document.getElementById('lefta').style.visibility="hidden";		
			
		});
      
	  
		
	  
        if(jQuery.browser.webkit!=true)
        {
            urlchange(location.pathname);
        }
        if(history.pushState)
        {
        window.addEventListener("popstate", function(e) {
            if(location.pathname!=base_url || location.pathname!= (base_url+'/') )
            {
		         
                urlchange(location.pathname);
            }
        });
}
        //$("#dragbar").hover(function(){showdrag();},function(){removedrag();});
        $("#dragbar").mouseenter(function(){showdrag();});
        $("#dragbar").mouseleave(function(){removedrag();});
        function showdrag()
        {
            $("#left").stop(true,false).animate({'margin-left':'0'});
            $("#right").stop(true,false).animate({'margin-left':'0'},function(){$("#innertxt").stop(true,false).animate({'opacity':'1'})});
        }

        function removedrag()
        {
            $("#innertxt").stop(true,false).animate({'opacity':'0'},function(){
            $("#left").stop(true,false).animate({'margin-left':'15'});
            $("#right").stop(true,false).animate({'margin-left':'-35'});
            })

        }
        
        function urlchange(href)
        {
if(start!=1)
{
            document.getElementById("tabcontent").innerHTML='<img src="'+ media_url + 'images/loading.gif" style="margin-left:280px;"/>';
            }
			if(href.charAt(href.length-1)=="/")
			{
				
				href=href.substring(0,href.length-1);
			}
			var hrefnew=href.split("/");
			var root=0;
			if(hrefnew[hrefnew.length-1]=="2012")
			{
			var dragto="Home";
			root=1;
			}
			else if(hrefnew[hrefnew.length-2]=="2012")
			{
			var dragto="Home";
			}
			else
			{
			var dragto=href.split("/")[href.split("/").length-2].replace(/_/gi," ").replace(/%20/gi," ");
			}
            
			
			
			
            $('.cat').each(function(){
              if($("#"+this.id+" p").text()==dragto)
              {
                document.getElementById(buttonon).src=media_url + "images/off.png";
                document.getElementById(this.id.replace("cat","p")).src= media_url + "images/on.png";
              buttonon=this.id.replace("cat","p");
                var pos=$(this).position();
                if(catleft+pos.left<0)
                {
                  while(catleft+pos.left<0)
                  {
                      catleft+=150;
                  }
				  if(catleft<=offsetleft) document.getElementById('righta').style.visibility="hidden";		
				  else document.getElementById('righta').style.visibility="visible";
				  if(catleft<0) document.getElementById('lefta').style.visibility="visible";	
				  else document.getElementById('lefta').style.visibility="hidden";
                  $("#categories").animate({'margin-left':catleft});
                }
                if(catleft+pos.left>=1190)
                {
                  while(catleft+pos.left>=1190)
                  {
                      catleft-=150;
                  }
				  if(catleft<=offsetleft) document.getElementById('righta').style.visibility="hidden";		
				  else document.getElementById('righta').style.visibility="visible";
				  if(catleft<0) document.getElementById('lefta').style.visibility="visible";	
				  else document.getElementById('lefta').style.visibility="hidden";
                  $("#categories").animate({'margin-left':catleft});
                }
                $("#dragbar").animate({'left':catleft+pos.left-20});
                

              }
            });
			if(root==1)
			{
			geteventdata("About");
			}
			else
			{
			
            geteventdata(href.split("/").pop().replace(/_/gi," "));
			}
			
        }

        $(".cat").click(
            function()
            {
                document.getElementById(buttonon).src=media_url + "images/off.png";
                document.getElementById(this.id.replace("cat","p")).src=media_url + "images/on.png";
              buttonon=this.id.replace("cat","p");
                var pos=$(this).position();
                $("#dragbar").animate({'left':catleft+pos.left-20});
                slideevents($("#"+this.id+" p").text());
                currentcat=$("#"+this.id+" p").text();
            }
        );



        $(".buttons").click(function(){
  
          document.getElementById(buttonon).src=media_url + "images/off.png";
                this.src=media_url + "images/on.png";
          buttonon=this.id;
                var id=this.id.replace("p","cat");
                var pos=$("#"+id).position();
          if(catleft+pos.left<0)
                {
                  while(catleft+pos.left<0)
                  {
                      catleft+=150;
                  }
				  if(catleft<=offsetleft) document.getElementById('righta').style.visibility="hidden";		
				  else document.getElementById('righta').style.visibility="visible";
				  if(catleft<0) document.getElementById('lefta').style.visibility="visible";	
				  else document.getElementById('lefta').style.visibility="hidden";
                  $("#categories").animate({'margin-left':catleft});
                }
                if(catleft+pos.left>=1190)
                {
                  while(catleft+pos.left>=1190)
                  {
                      catleft-=150;
                  }
				  if(catleft<=offsetleft) document.getElementById('righta').style.visibility="hidden";		
				  else document.getElementById('righta').style.visibility="visible";
				  if(catleft<0) document.getElementById('lefta').style.visibility="visible";	
				  else document.getElementById('lefta').style.visibility="hidden";
                  $("#categories").animate({'margin-left':catleft});
                }
                $("#dragbar").animate({'left':catleft+pos.left-20});
                slideevents($("#"+id+" p").text());
                currentcat=$("#"+id+" p").text();

        });
      
        function getevents(data)
        {
            var i=0;
          var htmlcontent="";
            //document.getElementById("events").innerHTML='<img src="' + media_url + 'images/loading1.gif" style="margin-top:30px;margin-left:550px;" />';
            //$.getJSON( base_url + "/getevents/",{cat:data},function(result){
            result=eventlist[data];
            document.getElementById("events").innerHTML="<ul id='eventsmenu'>";
            htmlcontent+="<ul id='eventsmenu'>";
            for(var temp in result)
		        {
              i++;
              //htmlcontent+='<li class="eventslist" id=list'+i+'><a +result[temp]+'</a></li>';
              htmlcontent+='<li class="eventslist"><a id=list'+i+' href="'+base_url+'/'+data+'/'+result[temp].replace(/\s/gi,"_")+'/'+'">'+result[temp]+'</a></li>';
            }
            htmlcontent+="</ul><span id='close'>Close</span>";
			
            document.getElementById("events").innerHTML=htmlcontent;
			$("#close").fadeIn();
           // });
			
			
			
        }

        function slideevents(id)
        {
			$("#close").fadeOut();
            $("#events").stop(true,false).animate({'margin-top':-200},"fast",'jswing',function(){document.getElementById("events").innerHTML="";getevents(id);}).animate({'margin-top':'0'},500,'easeInOutSine',function(){/*$("#close").css({'display':'block'});*/});
			
        }

        var poshome=$("#cat1").position();
        var hometemp=poshome.left;
        var tempX=0;
        var flag=0,flagleft=0;
        var temp;
        var currentcat="";

      var div=1;
        var html;
        $("#dragbar").css({'left':hometemp-20});
      
        $( "#dragbar" ).draggable({axis: "x",
        stop: function()
            {
                flag=0;
                var p,id,currentid,buttonid;
                var left=0;
                var temp1,temp2=0;
                var dragid=this.id;
                var pos=$("#"+dragid).position();
                tempX=pos.left;
                if(tempX>=0)
                {
                $('.cat').each(function(){
                        id=this.id;
                        p=$("#"+id).position();
                        temp1=p.left+catleft;
                        if((temp1<((tempX)+100))&&(temp2<(tempX))&&(tempX>=0))
                        {
                            left=temp1-20;
                            currentid=$("#"+id+" p").text();
                        }
                        else if((temp1>=((tempX)+100))&&(temp2<(tempX))&&(tempX>=0))
                        {
                            left=temp2-20;
                            currentid=$("#"+id+" p").text();
                        }
                        temp2=temp1;
                    });
                }
              else if(tempX<0)
                {
                      left=-20;
                      currentid="Home";
                }
                 $('.cat').each(function(){
                        id=this.id;
                        p=$("#"+id).position();
                        temp1=p.left+catleft;
                        if(left==temp1-20)
                        {
                            currentid=$("#"+id+" p").text();
                            buttonid=id.replace("cat","p");
                        }
                    });

                    if(left>1030)
                    {

                    	left=1030;

                    }
                $("#dragbar").animate({'left':left});
              document.getElementById(buttonon).src=media_url + "images/off.png";
              document.getElementById(buttonid).src=media_url + "images/on.png";
              buttonon=buttonid;
                slideevents(currentid);
                currentcat=currentid;
            },
          drag: function()
          {
           if(($(this).position().left>=1050)&&(catleft>=offsetleft)&&(flagleft==0))
                {
                  flagleft=1;
                  catleft=catleft-150;
				 if(catleft<=offsetleft) document.getElementById('righta').style.visibility="hidden";		
				 if(catleft<0) document.getElementById('lefta').style.visibility="visible";
                  $("#categories").animate({'margin-left':catleft},function(){flagleft=0;});
                }
          
           if(($(this).position().left<=20)&&(catleft<0)&&(flagleft==0))
                {
                  flagleft=1;
                  catleft=catleft+150;
				  if(catleft>offsetleft) document.getElementById('righta').style.visibility="visible";		
				  if(catleft==0) document.getElementById('lefta').style.visibility="hidden";
                  $("#categories").animate({'margin-left':catleft},function(){flagleft=0;});
                }
          }
          
        });


        $("#close").live('click',function(){$("#events").html("");$("#close").fadeOut();$("#events").animate({'margin-top':'-200'},500,'easeOutBounce');});


       $(".eventslist a").live('click',function(event){
if(history.pushState)
{
event.preventDefault();
}
           var id=this.id;
           var html=$("#"+id).text();
		   if(currentcat=="Home")
		   {
				if(html=="About")
				{
					history.pushState(null, null, base_url+"/");	
				}
				else
				{
					history.pushState(null, null, base_url+"/"+html.replace(/\s/gi,"_")+"/");	
				}
		   }
           else
		   {
				history.pushState(null, null, base_url+'/'+currentcat.replace(/\s/gi,"_")+"/"+html.replace(/\s/gi,"_")+"/");
		   }
		   
           geteventdata(html);
       });

        function geteventdata(html)
           {
				
               $("#highlight").fadeOut();
               $("#highlight2").fadeOut();
               
               $("#events").animate({'margin-top':'-200'},500,'easeOutBounce',function(){$("#close").fadeOut();});
               
              // $("#content"+div).css({'display':'none'});
               
             
			   if(start==1)
			   {
			   /*document.getElementById("titleofevent").innerHTML=tabresult["name"];
			   if(tabresult["name"]=="About")
					{
						document.title = "Apogee 2012";
					}
				else
					{
						document.title = "Apogee 2012 - " + tabresult["name"];
					}
					document.getElementById("catb").innerHTML = tabresult["category"];
		   document.getElementById("eveb").innerHTML = tabresult["name"];
		
					str="";
                   str='<ul class="tabslist"><li class="tabs" id="menulist0" style="margin-left:-300px">Overview</li>';
                   
				   
           */
            var counter=1;
             for(var temp in tabresult["tabs"])
		        {
              //str=str+'<li class="tabs" id="menulist'+counter+'" style="margin-left:-300px">'+temp+'</li>';
              counter++;
            }
            			start=0;
			var nhref = location.pathname;
				if(nhref.charAt(nhref.length-1)=="/")
				{
					nhref=nhref.substring(0,nhref.length-1);
				}
				var nhrefnew=nhref.split("/");
				if((nhrefnew[nhrefnew.length-1]=="2012"||nhrefnew[nhrefnew.length-2]=="2012")&&counter==1)
				{
					document.getElementById("content1").style.display="none";
					document.getElementById("highlighter").style.display="none";
					document.getElementById("tabcontent").style.width="800px";
					document.getElementById("tabcontent").style.marginLeft="170px";
					// document.getElementById("tabcontent").innerHTML=tabresult["overview"];
					 $("#tabcontent").fadeIn("slow");
					
				}
				else
				{
					document.getElementById("content1").style.display="block";
					document.getElementById("highlighter").style.display="block";
					document.getElementById("tabcontent").style.width="600px";
					document.getElementById("tabcontent").style.marginLeft="0px";
					//str=str+'<li class="lasthr" style="margin-left:-300px;cursor:default;"></li></ul>';
//                   document.getElementById("content1").innerHTML=str;
                   $("#content1").fadeIn();
		   $("#register").fadeIn();		
			//<hr style="width:200px;float:left;height:1px;">
                   
                   var tempid="";
                   var timedelay=100;
                   var count=0,tempcount=0;
                   $('.tabs').each(function(){
                      count++;
                   });
                   $('.tabs').each(function(){
                       tempcount++;
                       tempid=this.id;


                       if(tempcount!=count)
                       {
                           $("#"+tempid).animate({'margin-left':'0'},timedelay);
                       }
                       else
                       {
                           $("#"+tempid).animate({'margin-left':'0'},timedelay,function(){
                               //var pos=$("#menulist0").position();
                              // document.getElementById("tabcontent").innerHTML=tabresult["overview"];
                               $("#highlight").fadeIn();
                               $("#highlight2").fadeIn();
                               $("#highlight").css({'margin-top':'0','margin-left':'0'});
                               $("#highlight2").css({'margin-top':'0','margin-left':'0'});
                  //$("#tabcontent").css({'display':'block','margin-left':'400'});
                  $("#tabcontent").fadeIn("slow");
                           });
                           $(".lasthr").animate({'margin-left':'0'},timedelay+200);
                       }
                       timedelay=timedelay+200;
                   });
				   }
			   }
			   else
			   {
			     document.getElementById("content1").innerHTML="";
               $("tabcontent").fadeOut("slow");
               document.getElementById("titleofevent").innerHTML="";
               $("#register").fadeOut(500,function(){document.getElementById("register").innerHTML="";registerflag=0;$("#register").css({'cursor':'pointer'});});

                              
               document.getElementById("events").innerHTML="";
               var str="";
               document.getElementById("tabcontent").innerHTML='<img src="' + media_url + 'images/loading.gif" style="margin-left:280px;" />';
               $.getJSON(base_url + "/getcontent/",{even:html},function(result){
                   tabresult=result;
				   document.getElementById("titleofevent").innerHTML=tabresult["name"];
			   if(tabresult["name"]=="About")
					{
						document.title = "Apogee 2012";
					}
				else
					{
						document.title = "Apogee 2012 - " + tabresult["name"];
					}
					document.getElementById("catb").innerHTML = tabresult["category"];
		   document.getElementById("eveb").innerHTML = tabresult["name"];
                   str="";
                   str='<ul class="tabslist"><li class="tabs" id="menulist0" style="margin-left:-300px">Overview</li>';
                   var counter=1;
				   var nhref = location.pathname;
				
            for(var temp in result["tabs"])
		        {
              str=str+'<li class="tabs" id="menulist'+counter+'" style="margin-left:-300px">'+temp+'</li>';
              counter++;
            }
			if(nhref.charAt(nhref.length-1)=="/")
				{
					nhref=nhref.substring(0,nhref.length-1);
				}
				var nhrefnew=nhref.split("/");
				if((nhrefnew[nhrefnew.length-1]=="2012"||nhrefnew[nhrefnew.length-2]=="2012")&&counter==1)
				{
					document.getElementById("content1").style.display="none";
					document.getElementById("highlighter").style.display="none";
					document.getElementById("tabcontent").style.width="800px";
					document.getElementById("tabcontent").style.marginLeft="170px";
					document.getElementById("tabcontent").innerHTML=tabresult["overview"].replace('\\\'','\'');
					 $("#tabcontent").fadeIn("slow");
					
				}
				else
				{
					document.getElementById("content1").style.display="block";
					document.getElementById("highlighter").style.display="block";
					document.getElementById("tabcontent").style.width="600px";
					document.getElementById("tabcontent").style.marginLeft="0px";
					str=str+'<li class="lasthr" style="margin-left:-300px;cursor:default;"></li></ul>';
                   document.getElementById("content1").innerHTML=str;
                   $("#content1").fadeIn();
				
			
			//<hr style="width:200px;float:left;height:1px;">
                   
                   var tempid="";
                   var timedelay=100;
                   var count=0,tempcount=0;
                   $('.tabs').each(function(){
                      count++;
                   });
                   $('.tabs').each(function(){
                       tempcount++;
                       tempid=this.id;


                       if(tempcount!=count)
                       {
                           $("#"+tempid).animate({'margin-left':'0'},timedelay);
                       }
                       else
                       {
                           $("#"+tempid).animate({'margin-left':'0'},timedelay,function(){
                               //var pos=$("#menulist0").position();
                               document.getElementById("tabcontent").innerHTML=result["overview"].replace('\\\'','\'');

                               if(tabresult["register"]=='-1')
                               {
                               registerflag=1;
                               $("#register").css({'cursor':'default'});
                                                              
									 $("#register").html('<font color="green">You have registered.</font>');
										 $("#register").fadeIn();
										 }
								   	 else if(tabresult["register"])
									 {
									 $("#register").html('<img src="' + media_url + 'images/regevent.png" />');
									 $("#register").fadeIn();
                               							
                               																	 
									 }
                               
                               $("#highlight").fadeIn();
                               $("#highlight2").fadeIn();
                               $("#highlight").css({'margin-top':'0','margin-left':'0'});
                               $("#highlight2").css({'margin-top':'0','margin-left':'0'});
                  //$("#tabcontent").css({'display':'block','margin-left':'400'});
                  $("#tabcontent").fadeIn("slow");
                           });
                           $(".lasthr").animate({'margin-left':'0'},timedelay+200);
                       }
                       timedelay=timedelay+200;
                   });
					}

            });
			}
           }
        var tabcontent="";
        
        $("#register").live('click',function(){
        if(registerflag==0)
        {
        registerevent(tabresult["id"]);
}
        
        });
        
        $(".tabs").live('click',function()
        {

            var tabid=$(this).text();
            var id=this.id;
            var pos=$("#"+id).position();
            $("#highlight").animate({'margin-top':pos.top,'margin-left':'0'});
            $("#highlight2").animate({'margin-top':pos.top,'margin-left':'0'});
            //var tabid=this.id;

//document.getElementById("tabcontent").innerHTML="";

            $("#tabcontent").fadeOut(500,function(){
            tabcontent="";


            if(tabid!="Overview")
            {

            tabcontent=tabcontent+(tabresult["tabs"][tabid].replace('\\\'','\''));


                  document.getElementById("tabcontent").innerHTML=tabcontent;
                  //$("#tabcontent").css({'display':'block','margin-left':'400'});


            
            }
            else
            {
            tabcontent=tabcontent+(tabresult["overview"].replace('\\\'','\''));
            
                  document.getElementById("tabcontent").innerHTML=tabcontent;
                  //$("#tabcontent").css({'display':'block','margin-left':'400'});


            
            }
            $("#tabcontent").fadeIn(300);
});
        });
  })

