var safeNsf = 'snfw';

/* load optional script config start */
var customScript = false; // set to true to load custom script, e.g. for surveys
var scriptUrl = 'http://amch.questionmarket.com/adsc/d619181/2/667386/randm.js'; // source of custom script to load
/* load optional script config end */

// Custom Tab
function writeCustomTab(isPresent)
{				
	var theDiv = document.getElementById('custom_tab');
	var markup;
	if(isPresent == '1')
	{
		if(customScript == true){appendCustomScript(scriptUrl)} // leave this in for optional script calls
		
		markup = '&nbsp;';
		
		/*markup = '<a href="http://ad.doubleclick.net/clk;218200647;40865829;l?http://info.break.com/static/live/v1/pages/sponsors/bud-light-football/bud-light-football.html" target="_top"><img src="http://media1.break.com/static/live/v1/pages/sponsors/bud-light-football/images/budlight_tab.jpg" alt="Tailgate Approved" border="0" style="margin-top:-2px;" /></a>';
		// begin tracking pixel
		//ord=Math.random()*10000000000000000;
		if (typeof ord=='undefined') {ord=Math.random()*10000000000000000;}
		markup += '<iframe src="http://ad.doubleclick.net/adi/brk.cst/budlighttail_tab;sz=1x1;ord=' + ord + '?" width="1" height="1" marginwidth="0" marginheight="0" frameborder="0" scrolling="no">';
		if (navigator.userAgent.indexOf("Gecko")==-1){
			markup += '<script language="JavaScript" src="http://ad.doubleclick.net/adj/brk.cst/budlighttail_tab;sz=1x1;abr=!ie;ord=' + ord + '?" type="text/javascript"><\/script>';
		}
		markup += '</iframe>';*/
		// end tracking pixel 
	}
	else
	{
		markup = '&nbsp;'
	}
	theDiv.innerHTML = markup;
}

function appendCustomScript(url) {
	jQuery(function($) {
		$.getScript(url);
	});
}

// Subnav
jQuery(document).ready(function() {
	jQuery('.mainnavtrigger').hover(
		function() {
			jQuery(this).children('a:first-child').addClass('newnavselected');
			jQuery(this).children('.sbnv').show();
		},
		function() {
			jQuery(this).children('a:first-child').removeClass('newnavselected');
			jQuery(this).children('.sbnv').hide();
		}
	);
});

//age gate
jQuery(document).ready(function() {
    
    function isValidReferrer(){             
        var ref = document.referrer;         
        if (jQuery.cookie(safeNsf)){
            return (jQuery.cookie(safeNsf) === "y");
        }             
        if (ref == '') { return false; }
        var searchArr = ["google.com", "adon.com", "yahoo.com", "bing.com", "search.com", "aol.com", "ask.com", "msn.com", "altavista.com", "live.com", "lycos.com", "virgilio.com", "seznam.com", "cnn.com", "alltheweb.com"];
        var len = searchArr.length;
        for (var i=0; i<len; i++){                         
            if (ref.toLowerCase().indexOf(searchArr[i]) !== -1){               
                setNsf("y");
                return true;
            }            
        }        
        setNsf("n"); 
        return false;   
        
    }

    function isValidAge(){        
        var d = jQuery.cookie("dob");
        if (!d || d == 'undefined'){ return false; }
        
        var dSplit = d.split(" ")[0].split("/");        
        var uDate = new Date(parseInt(dSplit[2]),(parseInt(dSplit[0])-1),parseInt(dSplit[1]));
        var aDate = new Date().setFullYear(parseInt(new Date().getFullYear()) - 18);             
        return (aDate > uDate);        
    }
    
    (function spicyCheck(){	  
        var winLoc = window.location.href.toLowerCase();
        if (winLoc.indexOf("_nsfw") != -1){
            if (isValidReferrer()) { return; } /* this user is exempt and can proceed */
            if (IsLoggedIn()){
                if(!isValidAge()){	     	             
                    if (thirdPartyIsLoggedIntoBreak()){
                        if (winLoc.indexOf("agegateway_nsfw_thirdparty.html") == -1){
                            window.location.href = 'http://info.break.com/static/live/v1/pages/agegateway_nsfw_thirdparty.html';
                        }
                    }else{  		                                      
                        showSpicyReg(true);	                    
                    }   
                }
            }else{
               showSpicyReg(true); 
            }
        }
	 })();

	jQuery('.agegate-link').click(function() {		    	    
	    var theID = jQuery(this).attr('id');		       
	    if (isValidReferrer()) { return true; } /* this user is exempt and can proceed */	   
	    var urlPath = "";
	    if (IsLoggedIn()){		              
	        if (isValidAge()){	            
	            return true; 	        
	        }else{	            	           
	            if (thirdPartyIsLoggedIntoBreak()){
	                /* Go to new age-gate page for 3rd parties */	                			        
			        switch(theID) {
				        case 'spics-link':
				        case 'smovies-link':
				            window.location.href = 'http://info.break.com/static/live/v1/pages/agegateway_nsfw_thirdparty.html';					
					        break;
				        case 'gotd-link':
		                    window.location.href = 'http://info.break.com/static/live/v1/pages/ageverification_cookie_gotd.html';
		                    break;
	                    case 'gotm-link':
		                    window.location.href = 'http://info.break.com/static/live/v1/pages/ageverification_cookie_gotm.html';
		                    break;
					        					        
			        }			        
	            }else{ /* Not a valid age, but, they are logged in */
        			switch(theID) {
	                    case 'spics-link':
		                    window.location.href = 'http://info.break.com/static/live/v1/pages/agegateway_nsfw.html';					
		                    break;
	                    case 'smovies-link':
		                    window.location.href = 'http://info.break.com/static/live/v1/pages/agegateway_nsfw.html';
		                    break;
	                    case 'gotd-link':
		                    window.location.href = 'http://info.break.com/static/live/v1/pages/ageverification_cookie_gotd.html';
		                    break;
	                    case 'gotm-link':
		                    window.location.href = 'http://info.break.com/static/live/v1/pages/ageverification_cookie_gotm.html';
		                    break;
                    }	                
	            }	 
	            return false;       
	        } /* end of valid age check */ 
	    }else { /* Not logged in */ 
	       	                      	            			    
		    switch(theID) {				
			    case 'spics-link':
	            case 'smovies-link':
	            case 'gotd-link':
	            case 'gotm-link': 	                	               
                    showSpicyReg(false);                    			
				    break;
		    }			
			return false;	    
	    }	    
	
		/* Old code below */	
//		if (IsLoggedIn() && isValidAge()){
//			return true;
//		}
//		else {
//			var theID = jQuery(this).attr('id');
//			//alert(theID);
//			switch(theID) {
//				case 'spics-link':
//					window.location.href = 'http://info.break.com/static/live/v1/pages/ageverification_cookie_pictures_nsfw.html';					
//					break;
//				case 'smovies-link':
//					window.location.href = 'http://info.break.com/static/live/v1/pages/ageverification_cookie_movies_nsfw.html';
//					break;
//				case 'gotd-link':
//					window.location.href = 'http://info.break.com/static/live/v1/pages/ageverification_cookie_gotd.html';
//					break;
//				case 'gotm-link':
//					window.location.href = 'http://info.break.com/static/live/v1/pages/ageverification_cookie_gotm.html';
//					break;
//			}
//			return false;
//		} 
		
	});
	    
});


//jQuery(document).ready(function($){

//    
//    
//    (function checkNsfw(){
//       if (isLogout()) { return; } /* ignore the logout page */
//       var resKey = "_nsfw";
//       var anchorList = document.getElementsByTagName("a"); 
//       if (isValidReferrer()) { return; } /* this user is exempt and can proceed */
//       /* Check links */
//       if (IsLoggedIn()){
//            /* Now Check url */
//           if (window.location.href.toLowerCase().indexOf(resKey) != -1){
//                /* Popup Age-Gate Window, Hide Video and Blur Screen */
//                popupAgeGate();
//                return; /* no need to proceed further */
//	        }       
//       
//           for( var i = 0; i < anchorList.length; i++) 
//           {                    
//                if (anchorList[i].href.toLowerCase().indexOf(resKey) != -1){
//                    var anchorHref = anchorList[i].href;
//                    anchorList[i].href = "javascript:void(0)";
//                    anchorList[i].onclick = function(){
//                        showSpicyReg(anchorHref);
//                    };    		          		    		
//                }
//           }	    
//	    }else{ /* User is Logged In */
//	        var dob = $.cookie("dob");
//	        if (!thirdPartyIsLoggedIntoBreak()){
//	            if (window.location.href.toLowerCase().indexOf(resKey) != -1 && ){
//                    /* Popup Age-Gate Window, Hide Video and Blur Screen */
//                    popupAgeGate();
//                    return; /* no need to proceed further */
//	            }
//	        
//	        }else{ /* This is a 3rd party user */
//	        
//	        
//	        } /* End of testing whether 3rd party or regular login */	    
//	    
//	    } /* end if !IsLoggedIn() */
//    })();

//});


jQuery(function($){
	$('.feat-ico a').click(function(x){ // featured icon popup
	  x.preventDefault();
	  var w = window.open(
		'http://media1.break.com/static/live/v1/pages/pops/new-cat-promo.html',
		'popwin1','width=600,height=450,toolbar=no,resizable=1,scrollbars=0,toolbars=0,menubar=0'
	  );
	  w.focus();
	});
});

jQuery(function($) {
	$('#regSearchForm').bind('submit', function(e) {
		var searchTxt = $('#SearchTextbox1').val();
		var text = "";

		if (searchTxt == null || searchTxt == "") {
		  alert('Please enter a search term to perform search');
		  return false;
		}

		text = searchTxt;
		text = text.replace(" ", "-").replace("/", "");

		document.location.href = "http://www.break.com/findvideo/" + escape(text);

		return false;
	});
	$('input[id$=mainSearchButton]').bind('click', function() {
		var searchTxt = $('#SearchTextbox1').val();
		var text = "";

		if (searchTxt == null || searchTxt == "") {
		  alert('Please enter a search term to perform search');
		  return false;
		}

		text = searchTxt;
		text = text.replace(" ", "-").replace("/", "");

		document.location.href = "http://www.break.com/findvideo/" + escape(text);

		return false;
	});
});