jQuery.noConflict();
(function($) { 
  $(function() {
	  
Util = {};
Core = {};

/**
 * Redirect to a given location
 */
Util.redirect = function( href )
{
	window.location = href;	
};

/**
 * Set up partner list page. Inoked on document ready 
 */
Core.setupPartnerListPage = function()
{
	var $scrollingDiv = $(".product-filter");
	$(window).scroll(
		function()
		{
			//var top = ( $(window).scrollTop() - $scrollingDiv.height() < 0 )? 0 : $(window).scrollTop() - $scrollingDiv.height();
			var top = ( $(window).scrollTop() - 350 < 0 )? 0 : $(window).scrollTop() - 350;
			$scrollingDiv.stop()
			.animate({"top": top + "px"}, "slow" );
		}
	);
	
	$('.product-filter .list li').click(
		function(event)
		{
			// Prevent default link behavior
			event.preventDefault();
			
			// Get selected item
			var selCategory = $(this).attr('category');
			
			// Add selected class to the clicked option
			$('.product-filter .list li').removeClass('selected');
			$(this).addClass('selected');
			
			// Display all category titles when the 'all categories' option is clicked, hide otherwise
			if( selCategory == -1 )
			{
				$('.category-title').fadeIn();
			}
			else
			{
				$('.category-title').fadeOut();	
			}
			
			// Display category description only in the case of category_27 (fotovoltaiks)
			if( selCategory == 'category_27' )
			{
				$('.category-descr').fadeIn();
			}
			else
			{
				$('.category-descr').fadeOut();
			}	
			
			jQuery.each( $('.partnerList .partner'),
				function(index, elem)
				{
					var elem = $(elem);
					
					if( selCategory == -1 )
					{
						elem.fadeIn();
					}
					else
					{
						if( elem.attr('parent') == selCategory )
						{	
							elem.fadeIn();
						}
						else
						{
							elem.fadeOut();
						}
					}
				}
			);
			
			// Animate to top and set top of scrolling menu to 0
			$('html, body').animate( {scrollTop:0}, 'slow' );
			$scrollingDiv.stop().css( { 'top' : '0px' } );
		}
	);
};

/**
 * Set up contact us page. Inoked on document ready 
 */
Core.setupContactUsPage = function()
{
	// Setup google maps
	var myLatlng = new google.maps.LatLng(37.968106,23.7537);
	var myOptions={zoom:15,center:myLatlng,mapTypeId:google.maps.MapTypeId.ROADMAP};
	var map=new google.maps.Map(document.getElementById("map_canvas"),myOptions);
	var image=new google.maps.MarkerImage("/res/img/google-icon.png",
											new google.maps.Size(64,50),
											new google.maps.Point(0,0),
											new google.maps.Point(25,48));
	var marker=new google.maps.Marker({position:myLatlng,map:map,title:"Dendrinos",icon:image});
	
	// Place form in contactUsBox
	$('.contactUsFormBox .content').append( $('.contactUsForm').remove() );
};

Core.resizeSite = function()
{
	// Get browser window and body height
	var bodyH = $('body').height();
	var winH = $(window).height();
	
	// If browser window height is greater than body height, adjust
	// content area height, so that site will fit the window height
	if( winH > bodyH )
	{
		deltaH = winH - ($('.header-container').outerHeight(true) + $('.footer-wrapper').outerHeight(true) + $('.content').outerHeight(true)  );
		//$('.content').css('height', $('.content').height() + deltaH );
	}
};

//---------------------------
//Hook onload event
//---------------------------
$(document).ready(
	function()
	{
		// --------------------------------
		// Resize site 
		// --------------------------------
		Core.resizeSite();
		
		// If there is no active menu item, make home the active menu item
		if( $('.top-menu .menu li.active').size() == 0 )
		{
			// Activate the right home link depending on language
			if( $('.home').size() != 0 )
			{
				$('.top-menu .menu li.item27').addClass('active'); // english
				$('.top-menu .menu li.item42').addClass('active'); // greek
			}
		}
				
		/**
		 * hover intent config
		 */	
		var mouseOver = function()
		{
			$('.top-menu li.parent ul').stop().slideDown();
		};
		var mouseOut = function()
		{
			$('.top-menu li.parent ul').stop().slideUp();
		};
		var hiConfig = {
		   sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		   interval: 250, // number = milliseconds for onMouseOver polling interval
		   over: mouseOver, // function = onMouseOver callback (REQUIRED)
		   timeout: 500, // number = milliseconds delay before onMouseOut
		   out: mouseOut // function = onMouseOut callback (REQUIRED)
		};
		$('.top-menu li.parent').hoverIntent( hiConfig );
		
		// Set up contact page
		if( $('.contactUs').length )
		{
			Core.setupContactUsPage();
		}
		// Set up partner list page
		else if( $('.partnerList').length )
		{
			Core.setupPartnerListPage();
		}
		// Set up history page
		// NOTE:
		// ie does not support background-image-size css property;
		// to resize the gallery images properly change the src of 
		// each img
		else if( $('.history .gallery').length )
		{
			//Core.setupPartnerListPage();
			jQuery.each( $('.history .gallery img'), 
				function(index, elem)
				{
					elem = $(elem);
					var bgImg = elem.css('backgroundImage');
					bgImg = bgImg.replace('"', '');
					bgImg = bgImg.replace('"', '');
					
					bgImg = bgImg.substr( 4, (bgImg.length-5) );
					elem.attr('src', bgImg);
				} 
			);
		}
		else if( $('.home').length )
		{
			var descr1, descr2, descr3, descr4, descr5;
			if( $('#sessionLang').val() == 'en-GB' )
			{
				descr1 = '<a href="/en/services">Services</a>';
				descr2 = '<a href="/en/news">News</a>';
				descr3 = '<a href="/downloads_category30">Lighting</a>';
				descr4 = '<a href="/downloads_category31">Fotovoltaics</a>';
				descr5 = '<a href="/downloads_category35">Energy saving</a>';
			}
			else
			{
				descr1 = '<a href="/el/services">Υπηρεσίες</a>';
				descr2 = '<a href="/el/news">Νέα</a>';
				descr3 = '<a href="/downloads_category30">Φωτισμός</a>';
				descr4 = '<a href="/downloads_category31">Φωτοβολταϊκά</a>';
				descr5 = '<a href="/downloads_category35">Εξοικονόμηση ενέργειας</a>';
			}
			
			$(function() {
			    $("#de-showcase").showcase({
			        css: { width: "566px", height: "168px" },
			        animation: { type: "fade" },
			        images: [
			        { url:"/res/img/frontpage/fp-1.jpg",
			          description:descr1 },
			        { url:"/res/img/frontpage/fp-2.jpg",
			          description:descr2 },
			        { url:"/res/img/frontpage/fp-3.jpg",
			          description:descr3 },
			        { url:"/res/img/frontpage/fp-4.jpg",
			          description:descr4 },
			          { url:"/res/img/frontpage/fp-5.jpg",
				          description:descr5 }],  
			        navigator: {
			            position:"top-left",
			             orientation:"vertical",
			             css: {
			                padding:"6px",
			                margin:"25px 0px 0px 0px" }
			            },
			        titleBar: { css: { height:"25px" } }
			    });
			});

			// Display home page modules
			$('.home-page-modules').css('display', 'block');
		}
	}
);
$(window).resize( Core.resizeSite );
		
  });
})(jQuery);

