$(document).ready(function() {





$.fn.popOpen = function(){
		
		popID = $(this).attr('rel'); //Get Popup Name
		popURL = $(this).attr('href'); //Get Popup href to define size
		
		//Pull Query & Variables from href URL
		query= popURL.split('?');
		dim= query[1].split('&');
		popWidth = dim[0].split('=')[1]; //Gets the first query string value
		
		
		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) });
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
	};

	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
		$(this).popOpen(); //Run popOpen function on click
		return false;
	});
	
	
	
	//Close Popups and Fade Layer
	

	$('#yes').click(function() {
		$.cookie("legal-age","yes", {path: '/', expires: 7});
		$('#fade , .popup_block').fadeOut(); //fade them both out
		$('#fade').remove();
		return false;
	});
	
	
	$('#no').click(function() {
		document.location = "http://www.21stcenturysmoke.com/sorry";
	});
	
	
	if($.cookie("legal-age") == "yes"){
          
            //Do nothing, they are legal
        }
        else {
     
    $('a.poplight[href=#?w=350]').popOpen(); //Run popOpen function once on load   
        
        }





$("#main_nav ul li").not("#main_nav ul li#cart").not("#main_nav ul li#home").not("#main_nav ul li ul li").hover(
  function () {
    $(this).css('background-image', 'url(/images/main_nav_over.jpg)');
     $(this).css('cursor', 'pointer');
    
    
  }, 
  function () {
 $(this).css('background-image', 'none');
  
  }
);

$("#main_nav ul li ul li").not("#main_nav ul li#cart").not("#main_nav ul li#home").hover(
  function () {
    $(this).css('background-color', '#666');
     $(this).css('cursor', 'pointer');
    
  }, 
  function () {
 $(this).css('background-color', '#434346');
  
  }
);

$("#main_nav ul li#home").hover(
  function () {
   $(this).css('background-image', 'url(/images/main_nav_over.jpg)');
    $(this).children("a").children("img").attr("src", "/images/home_nav_icon_over.png");
    
    
  }, 
  function () {
  $(this).css('background-image', 'none');
	$(this).children("a").children("img").attr("src", "/images/home_nav_icon.png");
  
  }
);


$("#main_nav ul li#cart").hover(
  function () {
    $(this).children("a").children("img").attr("src", "/images/cart_nav_icon_over.png");
    
    
  }, 
  function () {
	$(this).children("a").children("img").attr("src", "/images/cart_nav_icon.png");
  
  }
);


$("#main_nav ul li").click(function(){
	  window.location=$(this).find("a").attr("href"); return false;
	});


$(".product_landing").hover(
  function () {
   $(this).animate({"opacity": 0.6}, 100);
  },
  function () {
$(this).animate({"opacity": 1}, 100);
 
  }
 
);




    $(".scroll").click(function(event){

        //prevent the default action for the click event

        event.preventDefault();

 

        //get the full url - like mysitecom/index.htm#home

        var full_url = this.href;


        //split the url by # and get the anchor target name - home in mysitecom/index.htm#home

        var parts = full_url.split("#");

        var trgt = parts[1];

 

        //get the top offset of the target anchor

        var target_offset = $("#"+trgt).offset();

        var target_top = target_offset.top;



        //goto that anchor by setting the body scroll top to anchor top

        $('html, body').animate({scrollTop:target_top}, 500);

    });






});



 var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();

