$(document).ready(function() {
    // making all menu links inactive on load. Next few lines will decide which
    // section (menu link) is active
    $('div.padding a').removeClass("active");
    $('ul#menu a').removeClass("active");
    // when new page is loaded, based on request URL it's choosen wich menu link should be active
    var pageName = getRequestedPageNameFromURL();

    if (pageName) {
        // selects side menu container div.padding
        $("div.padding").find('*[href*="' + pageName + '"]').each(function(index) {
            $(this).toggleClass("active");
        });

        // selects main menu container ul#menu
        $("ul#menu").find('*[href*="' + pageName + '"]').each(function(index) {
            $(this).toggleClass("active");
        });
    }

    // this will install flowplayer inside a tag with class myplayer
    if (pageName == "play_video.php") {
        flowplayer("a.myplayer", "swf/flowplayer-3.1.5.swf");
    }

    if (pageName == "hosts.faces" || pageName == "teamMembers.faces") {
        $("a.hostPhoto:even").css({"float" : "left", "margin" : "0px 20px 10px 0px"});
        $("a.hostPhoto:odd").css({"float" : "right", "margin" : "0px 0px 10px 20px"});
    }
	//search box
	$('#searchLink').click(function(event)
	{
		if ($('#query').val() != 'Search' &&
			$('#query').val() != 'Търси')
		{	
			window.location = "search.php?searchtext="+$('#query').val();
		}
		event.preventDefault();
	});
	$("#search label").inFieldLabels();
	$("form label").inFieldLabels();
	$('#query').keydown(function(event) {
		if (event.keyCode == 13)
			$('#searchLink').click();
	});


});

//var URLPatern = new Array("theShow", "hosts", "teamMembers", "partners", "contacts", "videos", "videosByType",
//            "photos", "blog", "producerBlog", "hostsBlog", "weeklyGames", "signUp", "quiz");
//var pageName = getURLByPattern(URLPatern);
function getURLByPattern(pattern) {
    for (var i = 0; i < pattern.length; i++) {
        //alert("pattern["+i+"] "+pattern[i]);
        var regex = new RegExp(pattern[i]);
        var result = regex.exec(window.location.href);
        //alert(result);
        if (result != null)
            return result;
    }
}

function getRequestedPageNameFromURL() {
    //var tmp = "http://comcom.com/sdsds/sdsds/_here_it_should_be.com";
    var URL = window.location.href;
    URL = URL.replace(/(.)*\//, "");
    URL = URL.replace(/[\?\&](.)*/, "");
    return URL;
}

function clearValue(field) {
    if (field.defaultValue == field.value)
        field.value = "";
}
function fillValue(field, text) {
    if (field.value == "")
        field.value = text;
}
