var $j = jQuery.noConflict();


$j(document).ready(function() {
	

	
////////////////////// ACCORDION ////////////////////////////////////////

	$j('#menu-accordionmenu').children('li').children('a').click(function(){
		
   		if(false == $j(this).next().is(':visible')) {
        	$j('#menu-accordionmenu').children('li').children('ul').slideUp(300);
			
			//$(this).removeClass('selected');
    	}
    	$j(this).next().slideToggle(300);
		//$(this).addClass('selected');
		return false;
	});
 
	//$j('#menu-accordionmenu ul:eq(1)').hide();
	//$j('#menu-accordionmenu').children('li:eq(0)').children('ul').hide();
	//$j('#menu-accordionmenu').children('li:eq(1)').children('ul').show();
	
	$j('#menu-accordionmenu').children('li').children('ul').hide();
	
	if ($j('.current-menu-ancestor').length){
		//alert('there is a selected');
	
		$j('#menu-accordionmenu').children('li.current-menu-ancestor').children('ul').show();
	} else {
		
		//alert('there is no selected');
		$j('#menu-accordionmenu').children('li:eq(1)').children('ul').show();
	}
	
////////////////////// END ACCORDION ////////////////////////////////////////	






////////////////////// COLLAPSABLE WIDGET ////////////////////////////////////////

$j('.widgetcontainer .widgettitle').children('a.toggle-list').click(function(){
		
		var thisObject = $j(this);
		var objectToSlide = $j(this).parent('.widgettitle').siblings('ul');

		if (objectToSlide.is(':visible')) {
			objectToSlide.slideToggle('fast', function() {
				thisObject.addClass('selected');
			});
		} else if (!objectToSlide.is(':visible'))  {
			objectToSlide.slideToggle('fast', function() {
				thisObject.removeClass('selected');
			});
		}
		
});




////////////////////// END COLLAPSABLE WIDGET ////////////////////////////////////////






////////////////////// ACCORDION WIDGET //////////////////////////////////////////////

// adding border when hovered
$j('.widget-subtitle a.category-link').mouseover(function() {
	$j(this).parent('.widget-subtitle').addClass('hovered');
	$j(this).prev('.toggle-sublist').addClass('border-outside');
});

$j('.widget-subtitle a.category-link').mouseout(function() {
	$j(this).parent('.widget-subtitle').removeClass('hovered');
	$j(this).prev('.toggle-sublist').removeClass('border-outside');
});


// adding collapsing behavior when clicking 

$j('.widgetcontainer .widget-subtitle').children('a.toggle-sublist').click(function(){
	
	var thisObject = $j(this);
	var objectToSlide = $j(this).parent('.widget-subtitle').siblings('ul.filter-subcategories');

		if (objectToSlide.is(':visible')) {
			objectToSlide.slideToggle('fast', function() {
				thisObject.addClass('selected');
			});	
		} else if (!objectToSlide.is(':visible'))  {
			objectToSlide.slideToggle('fast', function() {
				thisObject.removeClass('selected');
			});
		}
	
});





///////////////////// END ACCORDION WIDGET //////////////////////////////////////////


///////////////////// START - DSC INNUMBERS WIDGET //////////////////////////////////////////

$j('ul.xoxo li.widgetcontainer.widget_page_content_module_numbers .dscNumbers').mouseover(function(){
	
	$j(this).children('.hover-rectangle').css('display', 'block');
	
	
});



$j('ul.xoxo li.widgetcontainer.widget_page_content_module_numbers .dscNumbers').mouseout(function(){
	
	$j(this).children('.hover-rectangle').css('display', 'none');
	
	
	
});

$j('ul.xoxo li.widgetcontainer.widget_page_content_module_numbers_invert .dscNumbers-invert').mouseover(function(){
	
	$j(this).children('.hover-rectangle').css('display', 'block');
	
	
});



$j('ul.xoxo li.widgetcontainer.widget_page_content_module_numbers_invert .dscNumbers-invert').mouseout(function(){
	
	$j(this).children('.hover-rectangle').css('display', 'none');
	
	
	
});


///////////////////// END - DSC INNUMBERS WIDGET //////////////////////////////////////////


////////////////////// DROP DOWN MENU ////////////////////////////////////////


// this function is triggered on mouseover the submenu in order to keeep the background image of the menu element
// who refers to the menu item on which the mouseover was executed
		
		
		//variables holding the background positions for all menu elements and all states
		var posMenu1Hover = "-12px -54px";
		var posMenu2Hover = "-174px -54px";
		var posMenu3Hover = "-336px -54px";
		var posMenu4Hover = "-498px -54px";
	
	
		var posMenu1Normal = "-12px -6px";
		var posMenu2Normal = "-174px -6px";
		var posMenu3Normal = "-336px -6px";
		var posMenu4Normal = "-498px -6px";
	
	
		var posMenu1Selected = "-12px -108px";
		var posMenu2Selected = "-174px -108px";
		var posMenu3Selected = "-336px -108px";
		var posMenu4Selected = "-498px -108px";
		
		
		
		function keepBackground(who){
			if (who.hasClass('menu-item-626')){
				who.find("a:first-child").css("background-position", posMenu1Hover);
			} else if (who.hasClass('menu-item-622')){
				who.find("a:first-child").css("background-position", posMenu2Hover);
			} else if (who.hasClass('menu-item-621')){
				who.find("a:first-child").css("background-position", posMenu3Hover);
			} else if (who.hasClass('menu-item-620')){
				who.find("a:first-child").css("background-position", posMenu4Hover);
			}
		}
		
		
		
		
		// this function is triggered when the user performs a mouseout out of the the submenu 
		// in order to change the background image of the menu item to the image it had before the mouseover
		// who refers to the menu item on which the initial mouseover was executed
		
		function takeBackgroundAway(who){
		
			if (!who.hasClass('current-menu-item')){
				
				if (who.hasClass('menu-item-626')){
					who.find("a:first-child").css("background-position", posMenu1Normal);	
				} else if (who.hasClass('menu-item-622')){
					who.find("a:first-child").css("background-position", posMenu2Normal);
				} else if (who.hasClass('menu-item-621')){
					who.find("a:first-child").css("background-position", posMenu3Normal);
				} else if (who.hasClass('menu-item-620')){
					who.find("a:first-child").css("background-position", posMenu4Normal);
				}
				
				
			} else {
				
				if (who.hasClass('menu-item-626')){
					who.find("a:first-child").css("background-position", posMenu1Selected);	
				} else if (who.hasClass('menu-item-622')){
					who.find("a:first-child").css("background-position", posMenu2Selected);
				} else if (who.hasClass('menu-item-621')){
					who.find("a:first-child").css("background-position", posMenu3Selected);
				} else if (who.hasClass('menu-item-620')){
					who.find("a:first-child").css("background-position", posMenu4Selected);
				}
				
			}
			
		
		}

	
	
	
	
	
	
	var timeout    = 200;
	var closetimer = 0;
	var ddmenuitem = 0;
	var whichitem = 0;

	function jsddm_open() {  
		
		jsddm_canceltimer();
   		jsddm_close();
		
		if ($j(this).children(".subnav").length > 0){
			keepBackground($j(this));
   			
			//$j(this).children(".subnav").show("fast", function(){
				ddmenuitem =$j(this).children(".subnav").css("display", "block");
			//});
			
			
			//ddmenuitem = $j(this).children(".subnav").show("fast");
		}
	}

	function jsddm_close() {  
		if(ddmenuitem) {
			//if ($j(this).children(".subnav").length > 0){
				//ddmenuitem.css("display", "none");
				//
			//}
		//}
		
			//if (whichitem.css("display", "block")){
				//alert (ddmenuitem);
			if (whichitem.children(".subnav").length > 0){
				ddmenuitem.css("display", "none");
				//ddmenuitem.hide("fast");
				takeBackgroundAway(whichitem);
			}
		}
	}

	function jsddm_timer() {
		whichitem = $j(this);  
		closetimer = window.setTimeout(jsddm_close, timeout);
	}

	function jsddm_canceltimer() {  
		if(closetimer) {  
			window.clearTimeout(closetimer);
      		closetimer = null;
		}
	}

	
  	$j("div#menu-header ul#menu-mainnavigation").children("li").bind('mouseover', jsddm_open);
   	$j("div#menu-header ul#menu-mainnavigation").children("li").bind('mouseout',  jsddm_timer);

	document.onclick = jsddm_close;

	
	
	
	
	
	
	// rollover effects for the li items within the dropdown menus
	
	$j(".subnav li").mouseover(function() {
    	if ($j(this).hasClass("accent")){
			$j(this).removeClass("accent");
			
			
			if ($j(this).parent('ul').parent('div').hasClass('subnav-left')){
				$j(this).addClass("subnav-rollover-blue");
			
			} else if ($j(this).parent('ul').parent('div').hasClass('subnav-right')){
				$j(this).addClass("subnav-rollover-green");
				
			} else {
				$j(this).addClass("subnav-rollover-grey");
			}
			
			$j(this).addClass("ex-accent");
			
			$j(this).find("a").addClass("rollOver");
		} else {
			if ($j(this).parent('ul').parent('div').hasClass('subnav-left')){
				$j(this).addClass("subnav-rollover-blue");
			
			} else if ($j(this).parent('ul').parent('div').hasClass('subnav-right')){
				$j(this).addClass("subnav-rollover-green");
				
			} else {
				$j(this).addClass("subnav-rollover-grey");
			}
			
			$j(this).find("a").addClass("rollOver");
		}
	});
	
	
	$j(".subnav li").mouseout(function() {
		if ($j(this).hasClass("ex-accent")){
			$j(this).removeClass("ex-accent");
			
			
			if ($j(this).parent('ul').parent('div').hasClass('subnav-left')){
				$j(this).removeClass("subnav-rollover-blue");
			
			} else if ($j(this).parent('ul').parent('div').hasClass('subnav-right')){
				$j(this).removeClass("subnav-rollover-green");
				
			} else {
				$j(this).removeClass("subnav-rollover-grey");
			}
			
			$j(this).addClass("accent");
			$j(this).find("a").removeClass("rollOver");
		} else {
			if ($j(this).parent('ul').parent('div').hasClass('subnav-left')){
				$j(this).removeClass("subnav-rollover-blue");
			
			} else if ($j(this).parent('ul').parent('div').hasClass('subnav-right')){
				$j(this).removeClass("subnav-rollover-green");
				
			} else {
				$j(this).removeClass("subnav-rollover-grey");
			}
			$j(this).find("a").removeClass("rollOver");
		}
	});
	
	
	
	$j(".subnav li.subnav-title h1.dropdown-title a").mouseover(function() {
		$j(this).parent("h1.dropdown-title").parent("li.subnav-title").addClass("hovered"); 
	});
	
	$j(".subnav li.subnav-title h1.dropdown-title a").mouseout(function() {
		$j(this).parent("h1.dropdown-title").parent("li.subnav-title").removeClass("hovered"); 
	});
	
	
	
	
	
	
	
	
	
	
////////////////////////////////////////////////// GALLERY PROJECTS //////////////////////////////////////////////
		

		// on click: first, it sets all the other thumbnails to their normal appeareance, then highlites only the current thumbnail
		// then it changes the source of the big image and applies an animation effect to it 
		$j('.post-small-images a').click(function(){
				
			// turn off all other images
			$j(this).closest('.post-small-images').find('a').removeClass('selected');
			//$(this).closest('.thumbnailsGroup').find('img').fadeTo('fast', 1);
			//$(this).closest('.thumbnailsGroup').find('.thumbDesc').css('display', 'none');
			
			// turn on current image
			$j(this).addClass('selected');
			//$(this).fadeTo('fast', 0.2);
			//$(this).parent('a').siblings('.thumbDesc').css('display', 'block');
			
			var src= $j(this).children('img').attr('src');	
			var objectToChange = $j(this).closest('.post-small-images').siblings('.post-big-image').children('img');
			
			
			//alert(src);
			
			objectToChange.fadeTo('fast', 0.1, function() {
				objectToChange.attr('src', src);
				objectToChange.load(function() {
					objectToChange.fadeTo('fast', 1);
				});
			});
			
			
			return false; // to prevent the page from scrolling up when clicking

		});
		
		
		
		
////////////////////////////////////////////////////////////// END GALLERY PROJECTS /////////////////////////////////////////////////////////////////
	

	
});
