//stores the initial values of textboxes, only necesary because of IE
var defaultValues = new Array();

//takes an event and returns the sending node, for cross-browser
function getSender(e){
   return(e.target || e.srcElement);
}

//clears text input and stores as default text input value
function textbox_focus(e){
   e = e || window.event;
   var target = getSender(e);

   if(!target.id){
      target.id = "htmlelement_"+Math.floor(Math.random()*1000000);
   }
   if(!defaultValues[target.id]){
      defaultValues[target.id] = target.value;
   }
   if(target.value == defaultValues[target.id]){
      target.value = null || "";
   }
}

//restores the text input default value if the text box is empty
function textbox_blur(e){
   e = e || window.event;
   var target = getSender(e);
   if(!target.value){
      target.value = defaultValues[target.id] || "";
   }
} 


$(function() {
    
    // move the flyouts over a pixel in firefox
    if ($.browser.mozilla) $('.flyout').css('left',-201);

    $('#nav a.nav-item').mouseover(function(){
        flyout( $(this).parent(), true );
    });
    $('#nav a.nav-item').mouseout(function(){
        flyout( $(this).parent(), false );
    });
    $('.flyout').mouseover(function(){
        flyout( $(this).parent(), true );
    });
    $('.flyout').mouseout(function(){
        flyout( $(this).parent(), false );
    });
	
	$('#seo').live('click',function() { // enables the seo button
		page = $(this).attr('page_path')
		uri = $(this).attr('uri');
		website_ide = $(this).attr('website_ide');
		url = window.location.href;
		skybox('/admin/seo/webpage/seo-webpage-skybox',{ page_path: page, url: url, uri: uri, website_ide: website_ide });
	})
	
	/*photos photostrips */
	$(".photostrip").each(function(){
		var start = parseInt($('.start',this).val());
		$(this).jCarouselLiteMod({
			btnNext: ".photostrip_button_next",
			btnPrev: ".photostrip_button_prev",
			circular:true,
			start:start?start:0,
			speed: 200,
			visible:parseInt($('.photostrip_visible',this).val()),
			scroll: parseInt($('.photostrip_visible',this).val()),
			options:{
						photostrip_item_size: parseInt($('.photostrip_item_size',this).val()),
						photog_event_ide:$('.photog_event_ide',this).val(),
						album_skybox: $('#album_skybox').val()
					}
		});
    });
});

function flyout(item,show) {
    if (show) {
        $('.flyout',item).css('display','block');
        $('.nav-item',item).removeClass('nav-out');
        $('.nav-item',item).addClass('nav-over');
    } else {
        $('.flyout',item).css('display','none');
        $('.nav-item',item).removeClass('nav-over');
        $('.nav-item',item).addClass('nav-out');
    }
    Cufon.refresh("#nav a.nav-item");
}


function toggle_calendar(i){
   obj = $('#sidebar_calender_item_body_'+i);
   but = $('#sidebar_calender_item_button_'+i);

   //obj.css('display',obj.css('display')=='block'?'none':'block');
   obj.toggle();

   if(but.hasClass('showing')){but.removeClass('showing');}
   else{but.addClass('showing');}
}


function give_active(i){
   $('.calendar_peice_active').removeClass('calendar_peice_active');
   $('#calendar_peice_'+i).addClass("calendar_peice_active");

   $('.parties_description').hide();
   $('#parties_description_'+i).show();
}
/*photostrip mover*/
(function($) {
$.fn.jCarouselLiteMod = function(o) {
	var running = false;
	var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;
	var li = $("li", ul), itemLength = li.size(), curr = o.start;
	var liSize = width_css(li);   // Full li size(incl margin)-Used for animation
	//console.log(curr);
	var height = div.height();
	var btn_prev = $(o.btnPrev,div.parent());
	var btn_next = $(o.btnNext,div.parent());
	var btn_height = btn_prev.height()
	var margin_top = height/2-btn_height/2;
	var btn_css = 	{
						'display':'block',
						'height':height+'px'
					};
	btn_prev.css(btn_css).click(function() {
		return go(curr-o.scroll,'back',div);
	});
	btn_next.css(btn_css).click(function() {
		return go(curr+o.scroll,'forward',div);
	});
	function go(to,direction,obj) {
		if(!running) {
			//alert(to + '  ' + (itemLength-v));
			//console.log(to);
			if(to<0)
				return;
			if(to>=itemLength)
				return;
			if(to < itemLength-v){
				$(o.btnNext,$(obj).parent()).addClass("button_enabled").removeClass('button_disabled');
			}else{
				$(o.btnNext,$(obj).parent()).addClass("button_disabled").removeClass('button_enabled');
			}
			
			if(to-o.scroll<0){
				$(o.btnPrev,$(obj).parent()).addClass("button_disabled").removeClass('button_enabled');
				
			}else{
				$(o.btnPrev,$(obj).parent()).addClass("button_enabled").removeClass('button_disabled');
			}
			curr = to;
			running = true;
			ul.animate(
			   { left: -(curr*liSize) } , o.speed,
				function() {
					var upto = curr+2*(o.scroll);
					var pictures = $('li',div);
					//curr-o.visible is to check previous images in case we are going back
					for(var x = curr - o.visible; x<upto; x++){
						if(x>=0 && x<=pictures.length)
							request_pictures(div,pictures[x],o.options);
					}	
					//output('curr: '+curr+' itemLength: '+itemLength + ' to: '+to);
					running = false;
			   });
		}
		return false;
	};
};
function request_pictures(obj,picture,options){
	var media_item = $('.media_item_ide',$(picture));
	var ajaxpath = '/'+$('#market_slug').val()+'/photos/ajax/';
	if(media_item.length>0){
		if(!options)
			options = {};
		options.media_item_ide = media_item.val();
		$(picture).html("<center id = 'loading'><img src = '/images/loading2.gif'></center>");
		
		$.post(ajaxpath+'photostrip-image',options,function(data){
													$(picture).html(data);
												});
	}
}
function css(el, prop) {
    return parseInt($.css(el[0], prop)) || 0;
};
function width_css(el) {
    return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
};
})(jQuery);
/* end of photostrip mover */

