//======================================COOKIE FUNCTIONS

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
};

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
};

//======================================TWITTER POPUP ANIMATE FUNCTIONS

function twitter_hide() {
			$('a.twitter_popup').html('&#9650;<img src="/images/twitter_logo.png" alt="Twitter" />')
			$('a.twitter_popup').removeClass('active')
			$('#twitter_body').slideUp();
			$('#twitter_container').animate({width: '110'},400);
			$('ul#socialmedia').animate({width: '160'},400);
			$('.home a#scrollleft').animate({right: '110'},400);
			$('.home a#scrollright').animate({right: '20'},400);
}	
function twitter_show() {
			$('a.twitter_popup').html('&#9660;<img src="/images/twitter_logo.png" alt="Twitter" />')
			$('a.twitter_popup').addClass('active')
			$('#twitter_body').slideDown();
			$('#twitter_container').animate({width: '200'},400);
			$('ul#socialmedia').animate({width: '260'},400);
			$('.home a#scrollleft').animate({right: '330'},300);
			$('.home a#scrollright').animate({right: '240'},300);
}	


$(document).ready(function(){ 

logoclicks=0;$("h1#logo").mousedown(function(){logoclicks++;if(logoclicks==7){egg();}return false;});
function egg(){
$("#nav ul").css("background","transparent");
$("#nav").css("background","url('../images/samcropped2.jpg') bottom left no-repeat #000");}

///////////////////////////////////////////////////////////////////START SCROLL
		totalitems=$("ul#scrollcontent>li").size();
		itemwidth=$("ul#scrollcontent>li").width();
		rightmargin=$("ul#scrollcontent>li").css("marginRight").replace("px", "").replace("auto", "0");
		scrollwidth=$("#container").width()-$("#nav").width();
		containerwidth=$("#container").width();
		leftpadding=parseInt($("#scrollcontent").css("padding-left").replace("px", ""));
		contentwidth=totalitems*itemwidth;
		amountshowing=scrollwidth/itemwidth;
		limit=totalitems/Math.floor(amountshowing)-1;
		x=0; 
				
		$("div#scrollarea").css("overflow","hidden"); //removes scrollbars
		$("ul#scrollcontent").css("width",(itemwidth+rightmargin)*totalitems+leftpadding+"px"); //resize scrollcontent based on amount and width of items
		$("#mag_container ul#scrollcontent li").css("width", scrollwidth);
		$(".magazine ul#scrollcontent li").css("width", containerwidth);
		if(pageid!="contact"){
		$("#scrollarea").before('<span id="scroll"><a href="#" id="scrollleft" class="inactive '+pageid+'">&lt;</a> <a href="#" id="scrollright" class="'+pageid+'">&gt;</a></span>'); //adds controls
		}
		if(limit>0){	}else{		$("#scroll").hide();		}
		
		$("#scroll a")
		.mousedown(function(){if($(this).hasClass("inactive")){}else{$(this).css("margin-top","2px");$(this).css("margin-right","2px");}})
		.mouseup(function(){if($(this).hasClass("inactive")){}else{$(this).css("margin-top","0px");$(this).css("margin-right","0px");}});

		$("#scrollleft").click(function(){
			if(x>0){x--;skroll(x);$("#scrollright").removeClass("inactive");}
			return false;
			});
		
		$("#scrollright").click(function(){
			if(x<limit){x++;skroll(x);$("#scrollleft").removeClass("inactive");	}
			return false;
			});
			
			
	
	$('#note').animate({top:'220px', opacity: '1'}, {duration:300})
				.animate({opacity: '1'}, {duration:2500})
				.animate({top:'100px', opacity: '0'}, {duration:300});
	
	$('a#closepopup').live('click',function(){
		$('#overlay').fadeOut(300);
		$('.popup').hide();
		return false;
	});
	
	
	$('a[href$="video.swf"]').click(function(){
			$('div#container').after('<div id="overlay"></div><div id="video" class="popup"></div>');
			
			$('#overlay').fadeIn(300);
			
			var so = new SWFObject("/images/video.swf", "sotester", "359", "275", "1", "");
			so.write('video');
			$('.popup').addclosebutton();
			return false;
	});

	
	resetwidth();
	$(window).bind("resize", resizeWindow);
	function resizeWindow(e) {resetwidth();}

///////////////////////////////////////////////////////////////////TWITTER POPUP
	var twitter_teaseVar = readCookie('twitter_tease');
	$('#twitter_body a').each(function(){
		$(this).attr('target', '_blank');
	});

	if ($('#main').parent().attr('class') == 'home'){
		if (twitter_teaseVar == null){	
			$('.home #twitter_body').show();
			$('.home a#scrollleft').css('right', '330px');
			$('.home ul#socialmedia').css('width', '252px');
			$('.home #twitter_container').css('width', '200px');
			$('.home a#scrollright').css('right', '240px');
			$('.home a.twitter_popup').addClass('active')

			$('a.twitter_popup').toggle(function(){
				twitter_hide();
				createCookie('twitter_tease', 'no', 0);				
			}, function(){
				twitter_show();
				createCookie('twitter_tease', 'yes', 0);				
			});					
		}

		if (twitter_teaseVar == 'yes'){
			$('.home #twitter_body').show();
			$('.home #twitter_container').css('width', '200px');
			$('.home ul#socialmedia').css('width', '252px');
			$('.home a#scrollleft').css('right', '330px');
			$('.home a#scrollright').css('right', '240px');
			$('.home a.twitter_popup').addClass('active')
			$('.home a.twitter_popup').html('&#9660;<img src="/images/twitter_logo.png" alt="Twitter" />')
			$('a.twitter_popup').toggle(function(){	
				twitter_hide();
				createCookie('twitter_tease', 'no', 0);				
			}, function(){
				twitter_show();
				createCookie('twitter_tease', 'yes', 0);				
			});
		}
		if (twitter_teaseVar == 'no'){
			$('#twitter_body').hide();
			$('a.media_popup').addClass('maximize');		
			$('a.twitter_popup').toggle(function(){
				twitter_show();
				createCookie('twitter_tease', 'yes', 0);				
			}, function(){
				twitter_hide();
				createCookie('twitter_tease', 'no', 0);				
			});			
		}
	}
	
	else {
		$('a.twitter_popup').toggle(function(){
			twitter_show();
		}, function(){
			twitter_hide();
		});			
	}
	

////////////////////////////////////////////////////////////////////EMAIL NEWSLETTER SIGN UP

$("#newsletter form").submit(function(){
        formposition=$("form").index(this);
        thedata=$(this).serialize();
        themethod=$(this).attr("method");
        theurl=$(this).attr("action");
        $(this).find("input").attr("disabled","true");
		$(this).find("textarea").attr("disabled","true");
        $(this).css("opacity",".5");
        $.ajax({
            type: themethod,
            url: theurl,
            data: thedata,
            success: function() {
                $("form").eq(formposition).css("opacity","1");
                $("form").eq(formposition).find("p").html("Thank you! You have been added to our list.");
            }
        });
        return false;   
}); 



///////////////////////////////////////////////////////////////////CONTACT FORM 
$("form#contact").submit(function(){
        formposition=$("form").index(this);
        thedata=$(this).serialize();
        themethod=$(this).attr("method");
        theurl=$(this).attr("action");
        $(this).find("input").attr("disabled","true");
		$(this).find("textarea").attr("disabled","true");
        $(this).css("opacity",".5");
        $.ajax({
            type: themethod,
            url: theurl,
            data: thedata,
            success: function() {
                $("form").eq(formposition).css("opacity","1");
                $("form").eq(formposition).html("<p>Thanks for contacting Sam.</p><p> You should receive a reply shortly.</p>");
            }
        });
        return false;   
}); 

///////////////////////////////////////////////////////////////////ABOUT SAM
			 
		$(".about .ranking a").mouseover(function(){
			u=$(this).parent().parent().find("a").index(this);
			$(this).parent().parent().parent().find("tr").each(function(){
			$(this).find("td").eq(u).addClass("active");
			});
		});
			 
		$(".about .ranking td").mouseout(function(){
			$(".about .ranking td").removeClass("active");
		});
				
		$("td.western").mouseover(function(){
			$(this).find("span.western").show();
		});
		
		$("td.western").mouseout(function(){
			$(this).find("span.western").hide();
		});

///////////////////////////////////////////////////////////////////ABOUT SAM - MAGAZINE

		$('ul#sam_issues li p').css('opacity', 0);
		$('ul#sam_issues li').mouseenter(function(){
			$(this).find('p').animate({opacity: 1},300);
		}).mouseleave(function(){
			$(this).find('p').animate({opacity: 0},300);	
		});
///////////////////////////////////////////////////////////////////PROPERTIES
			
	$('.details #scrollcontent li>a[href$="jpg"]').fancybox({overlayColor: '#000',overlayOpacity:.75});
	$('.portfolio_details #scrollcontent li>a[href$="jpg"]').fancybox({overlayColor: '#000',overlayOpacity:.75});
	
	$("a.showmap").mousedown(function(){
		$('#map').addclosebutton();
		$("#overlay").show();
		$('#map').show();
		$('#map').css('visibility','visible');
		return false;
	});

	$("ul.buttons li a")
		.mousedown(function(){$(this).addClass("active")})
		.mouseup(function(){$(this).removeClass("active")})
		.mouseout(function(){$(this).removeClass("active")});
	
	
    //x=$(".otherrooms").html(); //x= the text in input field
    //x=x.replace(',','<br/>'); //strips spaces
	//$(".otherrooms").html(x);
		
	
///////////////////////////////////////////////////////////////////SHARE - EMAIL POPUP
	
	$("a#share").click(function(){
	$("ul.share_links").toggle();
	});

	
	$("a.share_email").click(function(){
	$("#email_popup").toggle();
	});

	$("a.closepopup").click(function(){
		$("#email_popup").hide();
	});	

	


	
	
});





$(document).keypress(function(e) {
	  switch(e.keyCode) { 
         case 37:         // left
			if(x>0){x--;skroll(x);$("#scrollright").removeClass("inactive");}
         break;
	    case 38:         // up
		
         break;
         case 39:         // right
			if(x<limit){x++;skroll(x);$("#scrollleft").removeClass("inactive");	}
         break;
	    case 40:         // down
			
         break;
		 }
});

jQuery.fn.addclosebutton = function() {
	return this.each(function(){
    $(this).prepend('<a id="closepopup" href="#">&#215;</a>');
  });
};


  

function skroll(x){
		scrollamount=itemwidth*x*Math.floor(amountshowing);
		$("#scrollcontent").animate({marginLeft:"-"+scrollamount+"px"}, {duration:800});
		
		if(x==Math.ceil(limit)){
		$("#scrollright").addClass("inactive");
		}
		if(x==0){
		$("#scrollleft").addClass("inactive");
		}
}


function resetwidth(){
		containerwidth=$("div#container").width();
		browserwidth=document.documentElement.clientWidth;
		//$("div#container").prepend(browserwidth+"    ");
		$("div#container").css("width","100%");
		if(browserwidth>1600){ $("div#container").css("width","1550px");}
}

