$(document).ready(function() {	


	// Hide RSS Promitheuti
	$(".rss-promitheuti").children(".display_feed").wrapAll('<div class="rss-promitheuti-content" />');


	// Remove links from level 1 menu
	$.each($(".sub-menu"), function() {
      	$(this).siblings("a").first().attr("href", "#");
      	$(this).siblings("a").first().css("cursor", "default");
	});
    
	
    // Automatically look for external links
    $.each($("a"), function () {
        var the_href = $(this).attr("href");
        if(typeof(the_href) != "undefined" && the_href.indexOf("javascript") == -1 && 
          ((the_href.indexOf(location.host) == -1 && the_href != "#") || the_href.indexOf(".pdf") != -1)) {
			  
            $(this).attr("rel", "shadowbox");  // youtube links automatically inserted as well (as they are external)
			
			if(the_href.indexOf(".pdf") != -1) {
				var basename = the_href.match(/[\/|\\]([^\\\/]+)$/);
				$(this).attr("title", "<b>" + basename[1] + "</b>");
			}
        }
    });
    
	
	
    // Shadowbox
	
	$(".blogroll a").attr("rel", "shadowbox");
    switch(lang_id) {
      case "el":
        $(".blogroll a").attr("title", "<b><span style='color: red'>&raquo;</span> βλέπετε σελίδα συνεργάτη</b>");
        break;
      case "en":
        $(".blogroll a").attr("title", "<b><span style='color: red'>&raquo;</span> you are viewing pages from partners</b>");
        break;
    }
	
	$(".rss-promitheuti a").attr("rel", "shadowbox");
	$(".rss-promitheuti a").attr("title", "");
    
	//$(".share_link a").attr("rel", "shadowbox");
	
	$('.postTabs_divs img[title|="click for slideshow"]').each(function () {
		var parent_tab = $(this).parent().parent();
		while(!parent_tab.hasClass("postTabs_divs")) {
			parent_tab = $(parent_tab).parent();
		}
		if($(this).parent().attr("href").indexOf(".pdf") != -1) {
			$(this).parent().attr("rel", "shadowbox");
		} else if ($(this).parent().attr("href").indexOf("youtube") != -1) {
			$(this).parent().attr("rel", "shadowbox[" + $(parent_tab).attr("id") + "];width=700;height=450");
			$(this).parent().attr("href").replace("watch?v=", "v/");
		} else {
			$(this).parent().attr("href").replace("watch?v=", "v/");
			
			if($(this).parent().attr("href").indexOf(".jpg") != -1) {
				var basename = $(this).parent().attr("href").match(/[\/|\\]([^\\\/]+)$/);
				$(this).parent().attr("title", "<b>" + basename[1].replace(".jpg", "") + "</b> " + $(this).attr("alt"));                
                $(this).parent().attr("rel", "shadowbox[" + $(parent_tab).attr("id") + "_img]");                
			} else if($(this).parent().attr("href").indexOf(".png") != -1) {
				var basename = $(this).parent().attr("href").match(/[\/|\\]([^\\\/]+)$/);
				$(this).parent().attr("title", "<b>" + basename[1].replace(".png", "") + "</b> " + $(this).attr("alt"));                
                $(this).parent().attr("rel", "shadowbox[" + $(parent_tab).attr("id") + "_img]");                
			} else {                
                $(this).parent().attr("rel", "shadowbox[" + $(parent_tab).attr("id") + "]");            
            }
		}
	});
	Shadowbox.setup(); 
    
	
    // Youtube images 
    // Link: http://www.youtube.com/watch?v=TIWrdglJS-0&feature=youtube_gdata    
    // Produce: http://img.youtube.com/vi/vdAxRJiSmk4/0.jpg
    var rssWidget = "#rss-4";
    var widgetLinks = $(rssWidget + " ul a");
    for(var i = 0; i < widgetLinks.length; i++) {
        var currHref = $(widgetLinks[i]).attr("href");
        var hrefVar = getHttpVar("v", currHref);
        $(widgetLinks[i]).prepend('<img class="youtubeImg" src="http://img.youtube.com/vi/' + hrefVar + '/0.jpg"/>');
    }
	
	
	// Rollover widgets
	$(".widget_moreinforeqform h3").toggle(function(e) {
		$(this).siblings(".widget-slide-content").first().stop().animate({height: $(this).siblings(".widget-slide-content").children().first().height() + "px", "padding-bottom": "12px"});
	  }, function () {
        $(this).siblings(".widget-slide-content").first().stop().animate({height: "0px", "padding-bottom": "3px"});
    });
	$(".category-more-links h3").toggle(function(e) {
		$(this).siblings(".category-items-content").first().stop().animate({height: $(this).siblings(".category-items-content").children().first().height() + "px", "padding-bottom": "12px"});
	  }, function () {
		$(this).siblings(".category-items-content").first().stop().animate({height: "0px", "padding-bottom": "3px"});
    });
	/*$(".rss-promitheuti h3").toggle(function(e) {
		$(this).siblings(".rss-promitheuti-content").first().stop().animate({height: $(this).siblings(".rss-promitheuti-content").children().first().height() + "px", "padding-bottom": "12px"});
	  }, function () {
        $(this).siblings(".rss-promitheuti-content").first().stop().animate({height: "0px", "padding-bottom": "3px"});
    });*/
    
	
	// Stamp last-menu-item after removing hidden items
	$.each($(".sub-menu, .menu-header .menu li"), function() {
		if($(this).hasClass("hiddenMenuItem")) {
			if($("body").hasClass("usergroup-su") && ($(this).hasClass("menu-item-1747") || $(this).hasClass("menu-item-1842"))) {
                // Suppliers
				$(this).show();
			} else if($("body").hasClass("usergroup-cu") && ($(this).hasClass("menu-item-1740") || $(this).hasClass("menu-item-1841"))) {
                // Customers
				$(this).show();
			}  else if($("body").hasClass("usergroup-pa") && ($(this).hasClass("menu-item-1854") || $(this).hasClass("menu-item-1855"))) {
                // Partners
				$(this).show();
			} else if($("body").hasClass("usergroup-admin")) {
                // Admin
				$(this).show();
			} else {
	        	$(this).remove();
			}
		}
    });
    $.each($(".sub-menu, .menu-header .menu"), function() {
        $(this).children("li").last().addClass("last-menu-item");
    });
	
	
	// Top search hover shows tags
	$(".top-search-form").hover( function () {
		$(".top-search-form-tags").show();
	  }, function () {
		$(".top-search-form-tags").hide();
	});
	
    
    // Hide erroneous <p> from RSS
    /*$(".display_feed").each(function () {
        for(var idx = 0; idx < $(this).find("p").length; idx++) {
            if($(this).find("p").eq(idx).attr("class") == "") {
                $(this).find("p").eq(idx).hide();
            }
        }
    });*/
    
	
	// Remove autocomplete tags view when the user starts typing in the search input box
    $(".ui-autocomplete-input").click( function () {
        $(".top-search-form-tags").remove();
    });
	
	
	// Replace footer images with black and white versions and setup hover function
	$(".partners-footer li a").each(function() {
		var orig_img = $(this).find("img").first().attr("src");
		var new_img = orig_img.replace(/.png/i, "_bw.png");
		$(this).find("img").first().attr("src", new_img);
		$(this).find("img").first().hover(function () {
			$(this).attr("src", orig_img);
		  }, function () {
			$(this).attr("src", new_img);
		});
	});
	
	
	$(".footer-map-cat .footer-map-posts").prev().toggle(function () {
		$(this).next().slideToggle();
	}, function () {
		$(this).next().slideToggle();
	});
	
	// Sharing buttons above tab navigator
	if($(".postTabs").length) {
		$(".postTabs").before($(".sharing-buttons"));
	}
	
	// Sharing buttons hover effect
	$(".share_link a, #site-description a").hover(function () {
		$(this).find(".social-bw").hide();
		$(this).find(".social-color").show();
	  }, function () {
		$(this).find(".social-bw").show();
		$(this).find(".social-color").hide();
	});
});

$(window).load(function () {
       
    $(".at300bs").each(function() {
        $(this).css("background", "url(images/widget32_bw.png) no-repeat scroll 0 0 transparent !important");
        $(this).hover(function () {
            $(this).css("background", "url(http://s7.addthis.com/static/r07/widget32.png) no-repeat scroll 0 0 transparent !important");
          }, function () {
            $(this).css("background", "url(images/widget32_bw.png) no-repeat scroll 0 0 transparent !important");
        });
    });
	
});


function getHttpVar( name, httpString )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( httpString );
  if( results == null )
    return "";
  else
    return results[1];
}
