﻿
// ********************************************************************** MASTER PAGE SCRIPT
// ****************************** header navigation
function nav_header()
{
    var options = $("#container_header ul li span");
    var optionIndex = 0;
    $("#container_header ul li span").animate({top:"0px", opacity:0.0}, 100, function(){
        optionIndex++;
        if (optionIndex == options.length)
        {   // when lights are hidden
            $("#container_header ul li a").each(function(){
                //alert(window.location.href.indexOf($(this).attr("href")) + "\r\n" + window.location.href + "\r\n" + $(this).attr("href"));
                if (window.location.href.indexOf($(this).attr("href")) != -1)
                {       // inactivate current navigation option
                    $(this).attr("href","#").click(function(){return false;}).css("color","#fff");
                    $(this).parent().children("span").stop().animate({opacity: 1.0}, 200);
                }
                else    // bind mouse enter & leave events for the rest of the navigation options
                    $(this).parent().bind("mouseenter",function(){
                        $(this).children("span").stop().animate({opacity: 1.0}, 200);
                    }).bind("mouseleave",function(){
	                    $(this).children("span").stop().animate({opacity: 0.0}, 400);
                    });
            });
        }
    });
}
// ****************************** list icon shine
function icon_shine()
{
    $("#home_page_ul li").bind("mouseenter",function(){
            $(this).children("em").stop().animate({opacity: 0.0}, 200);
        }).bind("mouseleave",function(){
            $(this).children("em").stop().animate({opacity: 1.0}, 200);
        });
}
// ****************************** right label link
function label_link()
{
    var newTop = $(window).height();
    newTop = newTop - $("#label_link").height();
    newTop = Math.round(newTop / 2);
    // weird bug... the label_link doesn't show correctly on the contact page (the only page with this problem)
    // so I just wait 1 second before animating the label_link and this appears to solve the problem
    setTimeout(function(){
        $("#label_link").animate({opacity:0.0, top:newTop + "px", left:"-100px"},100);
        $("#label_link").animate({opacity:1.0, left:"-10px"},500);
    }, 1000);
    
    $("#label_link").bind("mouseenter", function(){
        $(this).stop().animate({left:"0px"},200);
    }).bind("mouseleave",function(){
        $(this).stop().animate({left:"-10px"},200);
    });
}
// ****************************** language links
function language_links()
{
    // weird bug (again only on the contact page) (now only on the English version of the Contact page)
    // the language links dont't appear as they should (and as they do on other pages)
    // x(
    $("#languages a").animate({opacity:0.0}, 1, function(){$("#languages a").css("display", "block");});
    
    var current_lang;
    if (window.location.href.indexOf("/en") == -1)
    {
        $("#languages a.toggle_to_en").bind("mouseenter", function(){
            $(this).animate({opacity:1.0}, 100);
        }).bind("mouseleave", function(){
            $(this).animate({opacity:0.5}, 100);
        });
        $("#languages a.toggle_to_ro").css("cursor","default").animate({opacity:1.0},300);
        $("#languages a.toggle_to_en").animate({opacity:0.5},300);
    }
    else
    {
        $("#languages a.toggle_to_ro").bind("mouseenter", function(){
            $(this).animate({opacity:1.0}, 100);
        }).bind("mouseleave", function(){
            $(this).animate({opacity:0.5}, 100);
        });
        $("#languages a.toggle_to_ro").animate({opacity:0.5},300);
        $("#languages a.toggle_to_en").css("cursor","default").animate({opacity:1.0},300);
    }
}
// ********************************************************************** OTHER PAGES' SCRIPT

// ****************************** gallery
function setGallery()
{
    if ( typeof( window[ 'div.gallery' ] ) != "undefined" )
        $("div.gallery a").lightBox();
    else
        $("a.thumbnail").lightBox();
}
// ****************************** referinte > clienti
var rc_current_top;
var rc_clients;
var rc_clients_count;
var rc_visible_clients_count;
var rc_client_height;
var rc_direction = 1;      // -1 = up, 1 = down
var rc_min_top;
var rc_max_top;
var rc_steps = 3;
var rc_timer = 0;
function setClients()
{
    // on load > start animation
    rc_clients = $("ul.clienti li");
    rc_clients_count = rc_clients.length;
    rc_client_height = $("ul.clienti li:first").height();
    rc_visible_clients_count = Math.floor($("div.clienti_container:first").height() / rc_client_height);
    rc_min_top = -1 * rc_client_height * (rc_clients_count - rc_visible_clients_count);
    rc_max_top = 0;
    timer = setTimeout("setClients_goUpDown()", 1);
    // set click functions for up/down handles
    $("a.clienti_handle_up").click(function(){
        if(timer)
        {
            clearTimeout(timer);
            timer  = 0;
            rc_direction = 1;   // list goes down, view goes up
            timer = setTimeout("setClients_goUpDown()", 1);
        }
        return false;
    });
    $("a.clienti_handle_down").click(function(){
        if(timer)
        {
            clearTimeout(timer);
            timer  = 0;
            rc_direction = -1;   // list goes up, view goes down
            timer = setTimeout("setClients_goUpDown()", 1);
        }
        return false;
    });
}

function setClients_goUpDown(initTop)
{
    rc_current_top = $("ul.clienti").css("top").replace("px","");
    var rc_current_top_temp = rc_current_top * 1 + rc_direction * rc_client_height * rc_steps;
    // change direction if neccessary and reset temporary top
    if ((rc_current_top_temp > rc_max_top) || (rc_current_top_temp < rc_min_top))
    {
        if (rc_direction == -1)
        {
            if (rc_current_top_temp - rc_min_top < rc_client_height * rc_steps)
            {
                rc_direction = rc_direction * -1;
                rc_current_top_temp = rc_min_top;
            }
            else
            {
                rc_direction = rc_direction * -1;
                rc_current_top_temp = rc_current_top * 1 + rc_direction * rc_client_height * rc_steps;
            }
            $("a.clienti_handle_down").animate({opacity:0.2},10);
        }
        else
        {
            if (rc_max_top - rc_current_top_temp < rc_client_height * rc_steps)
            {
                rc_direction = rc_direction * -1;
                rc_current_top_temp = rc_max_top;
            }
            else
            {
                rc_direction = rc_direction * -1;
                rc_current_top_temp = rc_current_top * 1 + rc_direction * rc_client_height * rc_steps;
            }
            $("a.clienti_handle_up").animate({opacity:0.2},10);
        }
    }
    // reset handles to full opacity
    if (rc_current_top_temp < rc_max_top)
        $("a.clienti_handle_up").animate({opacity:1.0},10);
    if (rc_current_top_temp > rc_min_top)
        $("a.clienti_handle_down").animate({opacity:1.0},10);
    // animate list and recall function
    $("ul.clienti").animate({top: rc_current_top_temp},300);
    rc_current_top_temp2 = rc_current_top_temp;
    timer = setTimeout("setClients_goUpDown(rc_current_top_temp2)", 3000);
}

// ****************************** preturi > options' checkboxes

function preturi_option_changed()
{
    $(".preturi_options_chkbox").click(function(){
        var index = $(".preturi_options_chkbox").index(this);
        var checked = $(this).children("input[type='checkbox']").attr("checked");
        $("input[type='checkbox']").attr("checked", false);
        if (checked)
        {
            $(this).children("input[type='checkbox']").attr("checked", false);
            $("table.preturi_form").eq(index).hide();
        }
        else
        {
            $(this).children("input[type='checkbox']").attr("checked", true);
            $("table.preturi_form:not(eq('" + index + "'))").hide();
            $("table.preturi_form").eq(index).fadeIn("fast");
        }
    });
}

function setFocus()     // Page_ClientValidate so that it sets focus on the first not valid field
{
    if ( typeof( window[ 'Page_Validators' ] ) != "undefined" )
    {
        var refCtlId = null;
        var refCtl = null;
        var focusedInput = null;
        try {Page_ClientValidate();} catch(e) {}
        for (var i = 0; i < Page_Validators.length; i++)
        {
            //alert("Validator " + i + ": isValid = " + Page_Validators[i].isvalid + ", validates " + Page_Validators[i].controltovalidate);
            if (!Page_Validators[i].isvalid)
            {
                refCtlId = Page_Validators[i].controltovalidate;
                if ((refCtlId != null) && (refCtlId != ""))
                {
                    $("#" + refCtlId).addClass("invalid");
                    if (focusedInput == null)
                        focusedInput = refCtlId;
                }
            }
        }
        if (focusedInput != null)
        {
            document.getElementById(focusedInput).focus();
            //alert(focusedInput);
        }
        return (focusedInput == null);  // returneaza true daca totul e valid, false altfel
    }
}
    
function resetAfterInvalid()
{
    $(".input_text, .required, .invalid").blur(function(){
        if ($(".errors").css("display") == "block")
        {
            if ($(this).attr("value").length > 0)
                $(this).removeClass("invalid");
            // this next part is a bit evil:
            // if the user clicks "send" and the form is not valid
            // the user won't be able to do much until all the input is valid...
            // the user won't be able to change the language, the address bar location etc.
            // but then again, why would the user click "send" unless they really want to send the message?
            if (setFocus())     // may be considered an evil function from hell by some users who are just testing the form :P
                $(".errors").css("display","none");
        }
    });
}

// ****************************** produse si servicii

function adjustLeftRightHeights()
{
    // would be nice to do this via css, but the site is already rather heavy on javascript
    // so why waste time dealing with cross browser css when I have this pretty solution?  :D
    if ($("#ps_right").height() < $("#ps_left").height())
        $("#ps_right").height($("#ps_left").height());
}

/*   Web Creation START   */
function updatePortfolioContent(index, count)
{
	var spn = $("#portfolio_list li").eq(index).find("span");
	$(spn).animate({opacity:0.0},1,function(){$(spn).css("display","block"); $(spn).text((index * 1 + 1) + "/" + count); $(spn).animate({opacity:0.8},1000)});
	$("#portfolio h1").html(portfolio_titles[index]);
	$("#portfolio p").html(portfolio_description[index]);
	//updateProjectDetails(portfolio_xml_index[index]);    // AJAX
	updateProjectDetailsAJAX(portfolio_xml_index[index]);    // AJAX
}

function updateProjectDetailsAJAX(index)
{
    $.ajaxSetup({cache: false});
    var ajax_load = "<div id='portfolio_ajax_load'></div>";
    var loadUrl = "ajax_web_project_details.aspx";
    //$("#portfolio_project").html(ajax_load).load(loadUrl, "xml_index=" + index, function(){adjustLeftRightHeights();});
    // effects
    $("#portfolio_project_content")
        .slideUp(500, function(){
            $("#portfolio_project_content").load(loadUrl, "xml_index=" + index, function(){$("#portfolio_project_content").slideDown(500, function(){adjustLeftRightHeights();});});
        });
}

function doPortfolio()
{
    if ($("#portfolio_list_container").length > 0)      // check if portfolio exists
    {
	    // get the width of an item
	    var portfolio_width = $("#portfolio_list_container").css("width");
	    portfolio_width = portfolio_width.replace("px","") * 1;
	    // get the number of list items
	    var portfolio_count = $("#portfolio_list").find("li");
	    portfolio_count = portfolio_count.length;
	    // set the ul width
	    $("#portfolio_list").css("width", portfolio_count * portfolio_width + "px");
	    // current_portfolio
	    var current_portfolio = 0;
	    // max & min
	    var max_left = 0;
	    var min_left = 0 - (portfolio_count - 1) * portfolio_width;
	    var current_left = 0;
    	
	    updatePortfolioContent(current_portfolio, portfolio_count);
	}
	
	// click events
	$("#portfolio_left").click(function(){
		current_left = current_left + portfolio_width;
		if (current_left <= max_left)
		{
			current_portfolio--;
			$("#portfolio_list")./*stop().*/animate({left:current_left + "px"}, 300);
		}
		else
		{
			current_portfolio = portfolio_count - 1;
			current_left = min_left;    // 0
			$("#portfolio_list")./*stop().*/animate({left:min_left + "px"}, 300);
		}
		updatePortfolioContent(current_portfolio, portfolio_count);
		return false;
	});
	$("#portfolio_right").click(function(){
		current_left = current_left - portfolio_width;
		if (current_left >= min_left)
		{
			current_portfolio++;
			$("#portfolio_list")./*stop().*/animate({left:current_left + "px"}, 300);
		}
		else
		{
			current_portfolio = 0;
			current_left = max_left;
			$("#portfolio_list")./*stop().*/animate({left:max_left + "px"}, 300);
		}
		updatePortfolioContent(current_portfolio, portfolio_count);
		return false;
	});
}
/*   Web Creation END   */

// ****************************** MODAL WINDOW


// ****************************** masterpage script: calls header navigation and right label animation
function doStuff()      /* masterpage script */
{
    nav_header();
    label_link();
    language_links();
    icon_shine();
    var simpleModalLinks = $(".simplemodal");
    if (simpleModalLinks.length > 0)
        initSimpleModal();
}
