var swfu;

var FACEBOOK_VARS = {
	js_url: 'http://connect.facebook.net/ro_RO/all.js'
}


$(document).ready(function(){
	$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled - Adds empty span tag after ul.subnav
	
	$("ul.topnav li span").click(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

		$(this).parent().hover(function() {
		}, function(){	
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});

		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() { 
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
	
	$("#pbfooter-bar").jixedbar();
	$('.count_url').click(
		function(){
			url = SITE_URL+'/l.php?url='+escape($(this).attr('href'));
			window.open(url);
			return false;
		}
	);
	$('.feedb_button_push').click(
		function (){
			window.location.href=SITE_URL+'/contact.html';
		return false;
	});
	

	

	$('.box_comments_add').mouseover(
		function(){
			$(this).children('.box_comments_text').children('.mini_text').children('.report_text_button').css('visibility','visible');
			$(this).children('.delete_comm').children('a').css('visibility','visible');
		}
		
	);
	
	$('.delete_comm').mouseover(
		function(){
			$(this).css('background-color','#fff');
		}
		
	);
	
	$('.delete_comm').mouseout(
		function(){
			$(this).css('background-color','');
		}
		
	);
	

	
	
	
	$('.box_comments_add').mouseout(
		function(){
			$(this).children('.box_comments_text').children('.mini_text').children('.report_text_button').css('visibility','hidden');
			$(this).children('.delete_comm').children('a').css('visibility','hidden');
			
		}
		
	);
	
	
	$('.report_text_button > a').click(
		function(){
	
			return false;
		}
		
	);
	
	$('.invite_button_yahoo').popupWindow({
		windowURL:SITE_URL+'/inviter.html?action=take_user', 	
		height:500, 
		width:500, 
		centerBrowser:1 
	});
	
			
	
	if ($('#hedder_messages').length>0){
		setTimeout(function(){$('#hedder_messages').fadeOut('slow');},3000);
	}
	$('#status_dummy').focus(
		function() {
			$('#status_dummy').css('display','none');
			$('#status_text').css('display','');
			$('#status_button').css('display','');
			$('#status_text').focus();
		}
	);
	

	
	$('.input_clear').focus(function(){
		if ($(this).attr('rel')!='write'){
			$(this).val('');
			$(this).attr('rel','write');
			$(this).css({'color':'#000'})
		}
	});
	
	//$('.roll_over_bg').mouseover(function(){$(this).css({'border':'1px solid #D45F31'});});
	//$('.roll_over_bg').mouseout(function(){$(this).css({'border':'1px solid #fff'});});
	
	$('#add_to_jurnat_text').focus(function() {	
		$('#add_to_jurnat_text').css({'height':100,'color':'#000'});
		if ($('#add_to_jurnat_text').val()=='Ce ai facut azi...' || $('#add_to_jurnat_text').val()=='Lasa un mesaj...' )
		$('#add_to_jurnat_text').val('');
		$('#conf_jurnal').css('display','');
		$('#cancel_post_button_text').css('display','');
		$('#add_post_button_text').css('display','');
	});
	$('#add_to_jurnat_text').blur(function() {
			if ($('#add_to_jurnat_text').val()==''){
				$('#add_to_jurnat_text').css({'height':20,'color':'#8a8a8a'});
				$('#add_to_jurnat_text').val('Ce ai facut azi...');
				$('#conf_jurnal').css('display','none');
				$('#cancel_post_button_text').css('display','none');
				$('#add_post_button_text').css('display','none');
			}
	});
	$('#cancel_post_button').click(function() {
			
					$('#add_to_jurnat_text').css({'height':20,'color':'#8a8a8a'});
					$('#add_to_jurnat_text').val('Ce ai facut azi...');
					$('#conf_jurnal').css('display','none');
					$('#cancel_post_button').css('display','none');
	});
	
	
	$('.add_box_button').click(function(){
		$('.add_box_button').attr('style','');
		$(this).css({'color':'#464545','text-decoration':'none','cursor':'default'});
		$('#add_notes,#add_video,#change_status').css('display','none');
		$('#'+$(this).attr('rel')).fadeIn('slow');
		return false;
	});
	
	
	
	$('#b_h_register').click(function(){
		var error = false;
		error_msg = '';
		if ($('#h_email').val()==""){
			error = true;
		
		}
		if ($('#h_pass').val()==""){
			error = true;
			
		}
		if ($('#h_name').val()==""){
			error = true;
			
		}
		if ($('#h_s_name').val()==""){
			error = true;

		}
	
		if (!$('#h_ok_terms').is(':checked')){
			error = true;		
		}
		
		if (!$('#h_ok_news').is(':checked')){
			error = true;
		}
		
		if (error){
			pb_alert('<div>Eroare la inregistrare!</div>Toate campurile trebuie completate!');
		}else{	
			 $.post(SITE_URL+'/ajax.html', { 
					'action': 'register',
					'email' : $('#h_email').val(), 
					'category' : $('#h_category').val(), 
					'pass' : $('#h_pass').val(), 
					's_name' : $('#h_s_name').val(),
					'name' : $('#h_name').val(),
					'new_user' : 'add'
				},
			   function(data){
				if (data.registered==false){
						//$('#error_register div').html(data.error);
						pb_alert('<div>Eroare la inregistrare!</div>'+data.error);
						//$('#error_register').fadeIn('fast');
				}else{
						location.href =  data.user_url+'-informatii?open_edit=1';
				}
			     
			   }, "json");
		}
	
	});
	
	$('#h_l_pass_dummy').focus(function() {	
		//$('#h_l_pass').replaceWith('<input style="color:#000;" class="i_text" type="password" name="pass"  id="h_l_pass" value="" />');	
		$('#h_l_pass_dummy').css('display','none');
		$('#h_l_pass').css('display','');
		$('#h_l_pass').css('color','#000');
		$('#h_l_pass').focus();
	});
	$('#h_l_email').focus(function() {
		$('#h_l_email').val('');
		$('#h_l_email').css('color','#000');
	});
	
	$('#h_l_b_login').click(function(){
		$.post(SITE_URL+'/ajax.html', { 
				'action': 'login',
				'email' : $('#h_l_email').val(), 
				'pass' : $('#h_l_pass').val(),
				'keep_logedin': $('#h_l_keep_logedin').is(':checked')
			},
		   function(data){
			
			if (data==false){
				window.location.href=SITE_URL+'/login.html?error_id=1';	
				
			}else{
				location.reload();
			
			}
		     
		   }, "json");
	});
	
	$('#edit_avatr_button_over').mouseover(function(){
		$('#edit_avatr_button').css('display','block');
		
	});
	$('#edit_avatr_button_over').mouseout(function(){
		$('#edit_avatr_button').css('display','none');
		
	});
	
	
	$('#rasa_2').change(
		function (){
			$('#category_select').val($(this).val());
		}
	);
	var cache = jQuery.ajaxSettings.cache; 
			jQuery.ajaxSettings.cache = true;
			jQuery.getScript(FACEBOOK_VARS.js_url, function() {
				FB.init({appId: FB_API, status: true, cookie: true, xfbml: true});
			});
			jQuery.ajaxSettings.cache = cache;
			
			if ( $("#recommend_allover").length > 0 ) {
					if ($(window).width()<6000){
							$('#recommend_allover').css('visibility','visible');
					}
					p = $('#recommend_allover').position();
					intital_top2 = p.top;
					intital_top2 = parseInt(intital_top2);
					$(window).scroll(recommend_allover).resize(recommend_allover);				
				}
				
			if ( $("#recommend_allover_left").length > 0 ) {
				if ($(window).width()>=6000){
					$('#recommend_allover_left').css('visibility','visible');
				}
					p = $('#recommend_allover_left').position();
					intital_top = p.top;
					intital_top = parseInt(intital_top);
					$(window).scroll(recommend_allover_left).resize(recommend_allover);				
			}	
			
			$(window).resize(function() {
				if ($(window).width()>=6000){
					$('#recommend_allover_left').css('visibility','visible');
					$('#recommend_allover').css('visibility','hidden');
				}else{
					$('#recommend_allover_left').css('visibility','hidden');
					$('#recommend_allover').css('visibility','visible');
				}
			});
			
	
});


function recommend_allover(){	
	if (intital_top2<=$(window).scrollTop()){
		$('#recommend_allover').css({'position': 'fixed','margin-top':'0px','top':'0px','z-index':212122, 'width': $('#recommend_allover').width()});			
	}else{
		$('#recommend_allover').css({'position': '','margin-top':'20px'});
	}
}

function recommend_allover_left(){	
	if (intital_top<=$(window).scrollTop()){
		$('#recommend_allover_left').css({'position': 'fixed','margin-top':'0px','top':'0px','z-index':212122, 'width': $('#recommend_allover_left').width()});			
	}else{
		$('#recommend_allover_left').css({'position': '','top':'300px'});
	}
}




function edit_info(name){
	$('.i_edit_'+name+' > span').css('display','none');
	$('.i_edit_'+name+' > .i_text').css('display','inline');
	$('.i_edit_submit_'+name).css('display','inline');
	return false;	
}

function pb_alert(text){
	$('#pb_box_message > .content').html(text);
	$('#pb_box_message').fadeIn('fast');
	return false;
}


function connect_fb(){
	FB.login(function(response) {
	  if (response.session) {
	    if (response.perms) {
	      		FB.ui(
					{	
						method: 'stream.publish',
						message: 'iti prezinta profilul lui '+USER_INFOS.name+' pe Petsbox.ro '+USER_INFOS.url+'',
						attachment: {
							name: 'Petsbox.ro',
							caption: '{*actor*} iti prezinta profilul lui '+USER_INFOS.name+' pe Petsbook.ro',
							description: (
								'Si animalutul tau merita un profil pe Petsbox.ro '+SITE_URL+'!'
							),
							href: USER_INFOS.url
						},
						action_links: [
							{ text: 'Petsbox.ro', href: 'http://www.petsbox.ro' }
						],
						user_prompt_message: '!'
					},
					function(response) {
						if (response && response.post_id) {
							invite_ok(90);	
							close_invite_box();
						} else {
							invite_ok(2);	
							close_invite_box();
						}
					}  
				);
	    } else {
		  invite_ok(1);
		  close_invite_box();
	    }
	  } else {
		invite_ok(1);
		close_invite_box();	
	  }
	}, {perms:'email,publish_stream'});
	
}


function status_cancel(){
	$('#status_text').css('display','none');
	$('#status_button').css('display','none');
	$('#status_dummy').css('display','');
	return false;
}

function display_rase(id,id_update){
	$.post(SITE_URL+'/ajax.html', { 
			'action': 'get_rase',
			'id' : id
		},
	  function(data){
			options = '';
			//console.log(data);
			for (var i = 0; i < data.length; i++) {
	      	options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
			}
			if (options!=''){
			options = '<option>Alege</option>'+options;	
			$('#'+id_update).html(options);
			if (id_update == 'rasa_2'){
				$('#'+id_update).css('display','block');
			}
	  		$('#'+id_update).attr('disabled','');
			}else{
				$('#'+id_update).css('display','none');	
				$('#category_select').val(id);
			}
			
			    
	   }, "json");

	
	
	
	
}

function resend_confirm_email(){

	$.post(SITE_URL+'/ajax.html', { 
			'action': 'resend_confirm'
		},
	  function(data){
			if (data.error!=''){
				pb_alert(data.error);
			}else{
				pb_alert('E-mailul a fost trimis cu succes pe adresa de email <b>'+data.email+'</b>');
			}			    
	   }, "json");
	
	return false;
	
}

function report_item(item_type,id){
	
	$.post(SITE_URL+'/ajax.html', { 
			'action': 'report_item',
			'item_id' : id, 
			'item_type' : item_type
		},
	   function(data){
		if (data.error==''){
				pb_alert('Unul din operatori nostrii va verifica comentariul!<br /> Multumim!');
		}else{
				pb_alert(data.error);
		}
	     
	   }, "json");
	
	
	
	
	return false;
	
	
}


function change_avatar(){
	var html;
	html = '<div>';
	html += '<form method="post" id="set_Avatar" action="'+SITE_URL+'/profil/'+USER_ID+'.html-set_avatar"  enctype="multipart/form-data">Selectează un fisier in format imagine de pe calculatorul tau: <br /><input type="file" name="avatar" onchange="$(\'#set_Avatar\').submit()"/><input type="hidden" name="upload_avatar" value="upload_avatar" /></form>';
	html += '</div>';
	pb_alert(html);
	return false;
}

function play_video(id){
	$('#video_embed_'+id).css('display','');
	$('#video_text_'+id).css('display','none');
	$('#video_thumb_'+id).css('display','none');
	return false;
}

function insertAtCaret(areaId,text) { var txtarea = document.getElementById(areaId); var scrollPos = txtarea.scrollTop; var strPos = 0; var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) ); if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); strPos = range.text.length; } else if (br == "ff") strPos = txtarea.selectionStart; var front = (txtarea.value).substring(0,strPos); var back = (txtarea.value).substring(strPos,txtarea.value.length); txtarea.value=front+text+back; strPos = strPos + text.length; if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); range.moveStart ('character', strPos); range.moveEnd ('character', 0); range.select(); } else if (br == "ff") { txtarea.selectionStart = strPos; txtarea.selectionEnd = strPos; txtarea.focus(); } txtarea.scrollTop = scrollPos; return false;} 

function submit_message_manager(action){
	$('#action_form').val(action);
	submit_form = true;
	switch (action){
		case 'm_read':
		case 'm_noread':
		case 'm_del':
			
			if ($('input[class=slot_c]:checked').length==0){
				alert('Trebuie sa marchezi macar un mesaj');
				submit_form = false;
			}
			if (submit_form && action=='m_del' && !confirm('Chiar vrei sa stergi?')){
				submit_form = false;
			}
		break;
		case 'm_replay' :
		case 'm_forward' :
		if ($('input[class=slot_c]:checked').length==0){
			alert('Trebuie sa marchezi macar un mesaj');
			submit_form = false;
		}
		if ($('input[class=slot_c]:checked').length>1){
			alert('Doar un singur mesaj poti selecta pentru aceasta actiune!');
			submit_form = false;
		}
		break;
	}
	if (submit_form) $('#form_m').submit();
	return false;
}	
function checkbox(class_style,id){

	if ($('input[class='+class_style+']').is(':checked') && $(id).attr('checked')==false){
		$('input[class='+class_style+']').attr('checked', false);
		//$('input[class='+class+']').attr('disabled', false);
	}
	else{
		$('input[class='+class_style+']').attr('checked', true);
		//$('input[class='+class+']').attr('disabled', true);
	}
	


}

function send_new_rasaaaaa(){
	$.post(SITE_URL+'/ajax.html', { 
			'action': 'send_new_rasa',
			'new_rasa' : $('#new_rasa_text').val()
	},		 function(data){

	if (data!=false){
			$('#pb_box_message').hide();
			pb_alert ('Noua categorie a fost trimisa catre un moderator!<br /> In cel mai scurt timp va fi adaugata in lista!');
	}		
	});

}


function comment_show(id){
	$('#box_comment_dummy_'+id).remove();
	$('#wall_'+id).slideToggle('fast');
	$('#wall_'+id+' textarea').focus();
	return false;
}


function send_message (user_id){
	html = '<div class="send_msg_pop_up"><form action="'+SITE_URL+'/mesagerie.html?mod=compose" id="form_send_privat_message" method="post">';
	html+= '			<input type="hidden" name="email_id[]" id="for_id" value="'+user_id+'">';
	html+= '			<input type="hidden" name="back_to"  value="'+SITE_URL+'/profil/10001'+user_id+'.html">';
	html+= '			<table>';
	html+= '				<tbody><tr>';
	html+= '					<td>Subiect:</td><td><input type="text" class="i_text" name="subject" style="width: 300px;"></td>';
	html+= '				</tr>';
	html+= '				<tr>';
	html+= '					<td>Mesaj:</td><td><textarea class="i_text" name="message"></textarea></td>';
	html+= '				</tr>';
	html+= '				<tr>';
	html+= '					<td colspan="2">';
	html+= '						<button class="button" onclick="$(\'#form_send_privat_message\').submit();">Trimite mesajul</button>';
	html+= '					</td>';
	html+= '				</tr>';
	html+= '			</tbody></table>';
	html+= '		</form></div>';
	
	pb_alert (html);
	return false;
	
}



function alert_no_rasa(){
	
		
	var html;
	html = '<div>';
	html += '<form method="post" onsubmit="return false">Scrie rasa care doresti sa apara in lista in campul de mai jos: <br /><input type="text" name="send_rasa" id="new_rasa_text" /><br /><span class="mini_text">Ex: Caine - Fox terrier</span><br /><br /><input class="button" type="submit" name="send_new_rasa" onclick="send_new_rasaaaaa();" value="Trimite" /></form>';
	html += '</div>';
	pb_alert(html);
	return false;
}


function show_big_foto (img){
	html = '<div style="text-align:center"><img src="'+img+'" /></div>';
	pb_alert(html);
}
function inv_change(id_name){
	$('[id^="invite_box_"]').css('display','none');
	$('#invite_box_'+id_name).toggle();
	return false;
}

function close_invite_box(){
	$('#overlayer_invite').fadeOut('slow',function(){overlayer_stat('off');});
	return false;
	
}
function open_invite_box(){
	overlayer_stat('on');
	$('#overlayer_invite').fadeIn('slow');
	
	
}

function overlayer_stat (status){
	
	if ($('#overlayer').val()==undefined){

		$("body").append("<div id='overlayer'></div>");
	}
	$("#overlayer").css('width',$(document).width());
    $("#overlayer").css('height',$(document).height());
	$("#overlayer").css('background-color','#000');
	$('#overlayer').css('opacity', 0.70);
	$('#overlayer').css('position', 'absolute');
	$('#overlayer').css('z-index', '200');
	$('#overlayer').css('top', '0px');
	$('#overlayer').css('left', '0px');
	
	if (status=='on'){
		$('#overlayer').css('display', 'block');
	}else{
		$('#overlayer').css('display', 'none');
	}

}

function del_comment(comm_id){
	if (confirm('Chiar vrei sa iti stergi comentariul?')){
	$.post(SITE_URL+'/ajax.html', { 
			'action': 'del_comment',
			'comm_id' : comm_id
		},
	   function(data){
		if (data.error==''){
			$('#comment_'+comm_id).slideUp('fast',function(){pb_alert(data.ok)});			
		}else{
			pb_alert(data.error);
		}
	   }, "json");
	}
	return false;
}


function post_comment_ajax(wall_id){
	
	html='<div class="box_comments_add">';
	html+='<div class="box_comments_avatar">';
	html+='<a href="'+USER_INFOS.url+'"><img src="'+USER_INFOS.thumb_avatar+'" alt="'+USER_INFOS.name+'" title="'+USER_INFOS.name+'" /></a>';
	html+='</div>';
	html+='<div class="box_comments_text">';
	html+='<a href="'+USER_INFOS.url+'">'+USER_INFOS.name+'</a>  '+$('#w'+wall_id+'_ajax_comment').val();
	html+='<br />';
	html+='<div class="mini_text" style="padding-top:5px">cu 0 secunde in urma</div>';
	html+='</div>';
	html+='<div class="clearfloat">&nbsp;</div></div>';
	
	$.post(SITE_URL+'/ajax.html', { 
			'action': 'add_comment',
			'wall_id' : $('#w'+wall_id+'_ajax_wall_id').val(), 
			'item_type' : $('#w'+wall_id+'_ajax_item_type').val(), 
			'item_id' : $('#w'+wall_id+'_ajax_item_id').val(), 
			'comment' : $('#w'+wall_id+'_ajax_comment').val()
		},
	   function(data){
		if (data.error==''){
				$('#wall_'+wall_id).before(html);
				
				$('#w'+wall_id+'_ajax_comment').val('');
				$('#wall_'+wall_id).before('<div class="box_comments_add" id="box_comment_dummy_'+wall_id+'" onclick="return comment_show('+wall_id+'); "><input type="text" value="Adauga un comentariu..." style="font-size:12px; color:#ccc;height:15px;padding:3px;width:372px" /></div>')
				$('#wall_'+wall_id).css('display','none');
				//pb_alert(data.ok);
				//window.location.href='#conent_wall_'+wall_id;
		}else{
				pb_alert(data.error);
		}
	     
	   }, "json");
	
	
	
	
	return false;
}

function invite_ok(day){
	$.cookie('invite_pets', 'ok', { path: '/', expires: day });
	
}

/*START POPUP*/
(function($){ 		  
	$.fn.popupWindow = function(instanceSettings){
		
		return this.each(function(){
		
		$(this).click(function(){
		
		$.fn.popupWindow.defaultSettings = {
			centerBrowser:0, // center window over browser window? {1 (YES) or 0 (NO)}. overrides top and left
			centerScreen:0, // center window over entire screen? {1 (YES) or 0 (NO)}. overrides top and left
			height:500, // sets the height in pixels of the window.
			left:0, // left position when the window appears.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0, // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
			resizable:0, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			width:500, // sets the width in pixels of the window.
			windowName:null, // name of window set from the name attribute of the element that invokes the click
			windowURL:null, // url used for the popup
			top:0, // top position when the window appears.
			toolbar:0 // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
		};
		
		settings = $.extend({}, $.fn.popupWindow.defaultSettings, instanceSettings || {});
		
		var windowFeatures =    'height=' + settings.height +
								',width=' + settings.width +
								',toolbar=' + settings.toolbar +
								',scrollbars=' + settings.scrollbars +
								',status=' + settings.status + 
								',resizable=' + settings.resizable +
								',location=' + settings.location +
								',menuBar=' + settings.menubar;

				settings.windowName = this.name || settings.windowName;
				settings.windowURL = this.href || settings.windowURL;
				var centeredY,centeredX;
			
				if(settings.centerBrowser){
						
					if ($.browser.msie) {//hacked together for IE browsers
						centeredY = (window.screenTop - 120) + ((((document.documentElement.clientHeight + 120)/2) - (settings.height/2)));
						centeredX = window.screenLeft + ((((document.body.offsetWidth + 20)/2) - (settings.width/2)));
					}else{
						centeredY = window.screenY + (((window.outerHeight/2) - (settings.height/2)));
						centeredX = window.screenX + (((window.outerWidth/2) - (settings.width/2)));
					}
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else if(settings.centerScreen){
					centeredY = (screen.height - settings.height)/2;
					centeredX = (screen.width - settings.width)/2;
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + centeredX +',top=' + centeredY).focus();
				}else{
					window.open(settings.windowURL, settings.windowName, windowFeatures+',left=' + settings.left +',top=' + settings.top).focus();	
				}
				return false;
			});
			
		});	
	};
})(jQuery);
/*END POPUP*/
jQuery.cookie = function (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

