/*This method is used for tracking events on the website, by using Google Analytics Event Tracking interface.. */
function trackGa(category, action, url, moreData) {
    //Making sure the pageTracker exists before continuing..
    if (moreData) {
        return pageTracker._trackEvent(category, action, url, moreData);
    }
    return pageTracker._trackEvent(category, action, url);
        
}

function favAjax(e, img, id)
{
	var favPreloader;
	
	AjaxURL = '/aja/fav/'+id;
	new Ajax(AjaxURL, {
	method: 'get',
	onRequest: function(){
		favPreloader = new Element('img',{'class':'favPreloader','src':'/images/ajax-loader.gif', 'alt':'Ajax-Preloader'}).injectBefore(img);
	},
	onComplete: function(){
		
		if(undefined !== window.favs)
		{
			if (favs.length < 3)
			{
				favs.include(id);
				if (typeof reloadFavs=="function") reloadFavs();
			}
			else
			if (favs.contains(id))
			{
				favs.remove(id);
				if (typeof reloadFavs=="function") reloadFavs();
			}
			
			favPreloader.remove();
		}
		else
		{
			favPreloader.remove();
		}
		
		var response = this.response.text;
		action = ((response == '1' )? '_on' : '_off');
						
		if (window.ie6)
		{
			img.setStyles({
				filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/fav" +action+ ".png',sizingMethod='scale')"
			});
		}
		else
		{
			img.src = '/images/fav'+action+'.png';
		}
		
		e.name = ((response == '1') ? 'remove' : 'add');
		e.title = ((response == '1') ? 'הסר מהמועדפים' : 'הוסף למועדפים');
		img.alt = ((response == '1') ? 'הסר מהמועדפים' : 'הוסף למועדפים');
	}
	}).request();
}

function favsOff()
{
	var favsOff = $$('.fav_off');
	favsOff.each(function(e){
		var parent = e.getParent();
		var id = e.getElement('img').id;
		var width = parent.getSize().size.x-2;
		e.addEvents({
						
			'mouseenter': function(){
								
				if ($('off_'+id) != null) return;
				var warning = new Element('div', {'class': 'fav_off_warning', 'id': 'off_'+id}).injectBefore(e);
				var warningFx = new Fx.Styles(warning, {duration: 350, transition: Fx.Transitions.linear});
				
				warningFx.set({
					'width': width
				});
				
				warning.innerHTML = '<a href="/login"><strong>יש להתחבר על מנת לסמן מצגת כמועדפת</strong></a>';	
				
				warning.addEvent('mouseleave', function(){					
					
					(function(){
						warningFx.start({
							'opacity': 0
						});
						warning.remove();
					}).delay(500);			
					
				});
					
			}
		});
	});
}

function addFeatureJumpItemAjax(e, id){
	AjaxURL = '/aja/add_to_featured_main/'+id;
	new Ajax(AjaxURL, {
	method: 'get',
	onComplete: function(){		
		var response = this.response.text;
		action = ((response == '1' )? 'מצגת הוקפצה' : 'הקפץ מצגת');						
		
		e.innerHTML = action;
	}
	}).request();	
}

function initFeatureJumpItem(){
	var featureJump = $$('.featureJump');
	featureJump.each(function(e){
		var action;
		e.removeEvents();
		e.addEvents({
				'click' : function(){								
				addFeatureJumpItemAjax(e, e.name);															
			}
		});
	});
}

function fireFavs(specific)
{
	var favs = $$('.fav');
	favs.each(function(e){
	
		var action;
		var img = e.getElement('img');
		
		e.removeEvents();
		e.addEvents({
			
			'mouseenter': function (){
				action = ((e.name == 'add' )? '_on' : '_off');
				if (window.ie6)
				{
					img.setStyles({
						filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/fav" +action+ ".png',sizingMethod='scale')"
					});
				}
				else
				{
					img.src = '/images/fav'+action+'.png';
				}	
			},
		
			'mouseleave': function(){
				
				action = ((e.name == 'add' )? '_off' : '_on');
			
				if (window.ie6)
				{
					img.setStyles({
						filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/fav" +action+ ".png',sizingMethod='scale')"
					});
				}
				else
				{
					img.src = '/images/fav'+action+'.png';
				}
			},
			
			'click' : function(){
				
				var id = img.id.replace('fav_','');
				var id = id.replace('favUp_','');

				favAjax(e, img, id);
				
				if ($('present_id') && id == $('present_id').value)
				{
					if (e.name == 'remove')
					{
						// set Toolbar fav icon and text back to add mode
						$('fav').innerHTML = 'הוסף למועדפים';
						$('fav').name = 'add';
						$('fav_icon').src = '/images/btn_fav.png';
					}
					else
					if (e.name == 'add')
					{
						// set Toolbar fav icon and text back to add mode
						$('fav').innerHTML = 'הסר מהמועדפים';
						$('fav').name = 'remove';
						$('fav_icon').src = '/images/btn_fav_on.png';
					}
				}
				
				var elm = $('favUp_'+id);
				if (elm != null)
				{
					action = ((elm.name == 'add' )? '_on' : '_off');
					if (window.ie6)
					{
						elm.setStyles({
							filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/fav" +action+ ".png',sizingMethod='scale')"
						});
					}
					else
					{
						elm.src = '/images/fav'+action+'.png';
					}
				}
				
			}
		});
	});
} // enf fireFavs

window.addEvent('domready', function(){
	
	if ($('re_email'))
	{
		$('re_email').setStyle('display', 'none');
	}
	
	//run pngfix function
	pngfix('.pngfix');
	pngfix('.bage');
	
	fireFavs(); // init fireFavs
	favsOff(); // init favsOff
	initFeatureJumpItem(); //init featureJumps
	
	// build all buttons anim
	var buttons = $$('.btn_submit_switch');
	buttons.each(function(e){
		
		var eChild = ((e.getElement('img') == null)? e.getElement('input'): e.getElement('img'));  
		
		var height = e.getStyle('height').toInt();
		height = ((height == 25))? 30:22;
		e.addEvents({
			
			'mousedown': function(){
				eChild.setStyle('margin-top', -height + 'px');
			},
		
			'mouseup': function(){
				eChild.setStyle('margin-top', '0px');
			}
			
		});
		
	});
	
	// mod re_write search_form
	if($('search_submit')){
		$('search_submit').addEvent('click', function(){
			var keywords = $('search_form').getElement('#search_bar').value;
			document.location.href = '/results/'+keywords;
		});
		
		$('search_bar').addEvent("keydown",(
			function(ev){
			if (ev.key == 'enter')
			{
				var keywords = $('search_form').getElement('#search_bar').value;
				document.location.href = '/results/'+keywords;
			}
		}).bindWithEvent(document));
	}
	
	// building msg_block
	var msgBlocks = $$(".floated_msg");
	var i=0;
	msgBlocks.each(function(element){
		content = $(element.id).innerHTML;
		element.empty();
		
		//var block_close = new Element ('div',{"class":"block_close"}).injectInside(element);
		//var block_close_img = new Element ('img',{"id":"block_close_img"+i, "class":"pngfix", "src": "/images/msg_block_close.png", "alt": "Close"}).injectInside(block_close);
		
		var block_pick = new Element ('div',{"class":"block_pick"}).injectInside(element);
		var block_pick_img = new Element ('img',{"id":"msg_block_pick"+i, "class":"pngfix", "width": "8", "height" : "19", "src": "/images/msg_block_pick.png", "alt": "Pick"}).injectInside(block_pick);
		
		var block_top = new Element ('div',{"class":"block_top"}).injectInside(element);
		var block_top_img = new Element ('img',{"id":"msg_block_top"+i, "class":"pngfix", "width": "240", "height" : "10", "src": "/images/msg_block_top.png", "alt": "top"}).injectInside(block_top);
		
		var block_middle = new Element ('div',{"id": element.id+"_p", "class":"block_middle"}).injectInside(element);
		var block_content = new Element ('p',{}).injectInside(block_middle);
		block_content.setHTML(content);
		
		var block_bottom = new Element ('div',{"class":"block_bottom"}).injectInside(element);
		var block_bottom_img = new Element ('img',{"id":"msg_block_bottom"+i, "class":"pngfix", "width": "240", "height" : "10", "src": "/images/msg_block_bottom.png", "alt": "bottom"}).injectInside(block_bottom);
	i++;
		
		pngfix('.pngfix');
	});
	
	// paint inputs for IE & submit on enter fix
	if (window.ie6)
	{
		var inputs = $$('input');
		inputs.each(function(e){
			e.addEvents({
				
				'focus': function(){
					// canel coloring if username_error;
					if (e.id == 'username' && username_error == true) return
					
					e.setStyle('background-color','#ebfde5');
				},
				
				'blur': function(){
					// canel coloring if username_error;
					if (e.id == 'username' && username_error == true) return
					
					e.setStyle('background-color','#ffffff');
				}
			});
		});
		
		var textareas = $$('textarea');
		textareas.each(function(e){
			e.addEvent('focus',function(){
				e.setStyle('background-color','#ebfde5');
			});
			
			e.addEvent('blur',function(){				
				e.setStyle('background-color','#ffffff');
			});
			
		});
			
		/*
var common_form = $$('.common_form, .shortinput');
		common_form.each(function(e){
			e.addEvent("keydown",(
				function(ev){
				if (ev.key == 'enter')
				{
					//alert(e.getParent() + ' will be submited');
					e.getParent().submit();
				}
			}).bindWithEvent(document));
		});
*/
		
	}
		
	// init setup;
	var menu_buttons = $$('.menu_button');
	var new_menu_buttons = new Array();
	var newSrc;
	
	//preload the _over images;
	var i = 0;
	menu_buttons.each(function(e){
		new_menu_buttons[i] = e.src.replace(/.png/,'_over.png');
		++i;
	});
	
	new Asset.images(new_menu_buttons, {});
	
	menu_buttons.each(function(e){
		e.addEvent('mouseenter', function(){
			if (!e.src.contains('_over'))
			{
				newSrc = e.src.replace(/.png/,'_over.png');
				//console.info(newSrc);
				e.src = newSrc;
			}
		});
		
		e.addEvent('mouseleave', function(){
			if (e.src.contains('_over'))
			{
				newSrc = e.src.replace(/_over.png/,'.png');
				//console.info(newSrc);
				e.src = newSrc;
			}
		});
	});
		
	var moreTrigers = $$('.more');
	moreTrigers.each( function(el){
		el.addEvent('click', function(){
			elStatus = $(el.id+'_target').getStyle('display');
			if (elStatus == 'none')
			{
				$(el.id+'_target').setStyle('display','inline');
				$(el.id).innerHTML = '(הסתר...)';
			}
			else
			{
				$(el.id+'_target').setStyle('display','none');
				$(el.id).innerHTML = '(עוד...)';
			}
		})
	});
	
});

window.addEvent('load',function(){
	
	new Asset.images(['/images/menu_categories_over.png', '/images/menu_main_over.png', '/images/menu_my_presents_over.png', '/images/menu_presents_over.png'], {});
		
});
