var iDOMLoaded = false;
var isIE = /MSIE (5\.5|6\.|7\.)/.test(navigator.userAgent) && navigator.platform == "Win32";

jQuery(document).ready(function() {
    iDOMLoaded = true;

    /*if($(".dopngfix")[0]) {
    $(document).pngFix();
    }/**/

    // Clients on index page
    if ($(".clients")[0]) {
        $(".clients a img").each(
			function() {
			    if (isIE) {
			        var prnt = $(this).parent("a");
			        prnt.css("width", "200px");
			        prnt.css("height", "70px");
			        //prnt.css("opacity", 0.5);

			        var src = this.src;
			        var srcbig = $(this).attr("lowsrc");
			        this.src = "/files/images/blank.gif";
			        this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
			        this.style.width = "100%";
			        this.style.height = "100%";

			        var cln = $(this).clone().insertBefore($(this));
			        cln.attr("src", "/files/images/blank.gif");
			        cln.attr("className", "cln");
			        cln.get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + srcbig + "',sizingMethod='scale')";
			        cln.css({ 'width': '100%', 'height': '100%', 'display': 'none' });
			    } else {
			        var cln = $(this).clone().insertBefore($(this));
			        cln.attr("src", $(this).attr("lowsrc"));
			        cln.attr("className", "cln");
			        cln.css({ 'position': 'absolute', 'visibility': 'hidden' });
			    }
			}
		);
        $(".clients li").hover(
	    	function() {
	    	    var lnk = $(this).children("a");
	    	    var orig = lnk.children("img:not(.cln)");
	    	    var cln = lnk.children("img.cln");
	    	    cln.stop();
	    	    if (isIE) {
	    	        cln.css("display", "block");
	    	        orig.css("display", "none");
	    	        lnk.css("width", "160px");
	    	        lnk.css("height", "56px");
	    	        lnk.css("marginTop", "7px");
	    	        lnk.css("marginLeft", "20px");
	    	        lnk.css("opacity", 0.5);
	    	        lnk.animate({ width: "200px", height: "70px", marginLeft: "0", marginTop: "0", opacity: 1 }, 150);
	    	    } else {
	    	        cln.css("width", "160px");
	    	        cln.css("height", "56px");
	    	        cln.css("marginTop", "7px");
	    	        cln.css("marginLeft", "20px");
	    	        cln.css("opacity", 0.5);
	    	        cln.css("visibility", "visible");
	    	        orig.css("visibility", "hidden");
	    	        cln.animate({ width: "200px", height: "70px", marginLeft: "0", marginTop: "0", opacity: 1 }, 150);
	    	    }
	    	},
	    	function() {
	    	    var lnk = $(this).children("a");

	    	    if (isIE) {
	    	        lnk.stop();
	    	        lnk.animate({ width: "160px", height: "56px", marginLeft: "20px", marginTop: "7px", opacity: 0.5 }, 150,
		    				function() {
		    				    var orig = $(this).children("img:not(.cln)");
		    				    var cln = $(this).children("img.cln");
		    				    orig.css("display", "block");
		    				    cln.css("display", "none");
		    				    $(this).css("opacity", 1);
		    				    $(this).css("width", "200px");
		    				    $(this).css("height", "70px");
		    				    $(this).css("marginTop", "0");
		    				    $(this).css("marginLeft", "0");
		    				}
		    		); /**/

	    	    } else {
	    	        var cln = lnk.children("img.cln");
	    	        cln.stop();
	    	        cln.animate({ width: "160px", height: "56px", marginLeft: "20px", marginTop: "7px", opacity: 0.5 }, 150,
		    				function() {
		    				    var orig = $(this).siblings("img");
		    				    orig.css("visibility", "visible");
		    				    $(this).css("visibility", "hidden");
		    				}
		    		); /**/
	    	    }
	    	}
	    );
    }


    // Works on index page
    if ($("#prev_work a")[0]) {
        $('#fisheye').Fisheye(
			{
			    maxWidth: 20,
			    items: 'a',
			    itemsText: 'span',
			    container: '#small_works_list',
			    scrollContainer: '#prev_work',
			    scrollOverHeight: 60,
			    scrollOverTop: 10,
			    scrollOverWidth: 350,
			    itemWidth: 40,
			    proximity: 90,
			    speed: 10,
			    halign: 'left'
			}
		);

    } /**/


    // Right menu on regular pages
    if ($(".main_menu")[0]) {
        $(".main_menu li:not(.select) ul").hide();

        // By Over
        $(".main_menu a").attr("id", function(arr) { return "li-a-id" + arr; });
        $(".main_menu")[0].bProcDo = false;
        $(".main_menu")[0].bOutProcDo = false;


        // By Click
        $(".main_menu li a").click(function() {
            if (this.parentNode.parentNode.className == "main_menu") {
                if ((this.href.match("#$") == "#") && $("ul:hidden", $(this).parent()).get(0)) {
                    $(".main_menu li ul:visible").slideUp("slow");
                    $(this).next().slideDown("slow");
                    return false;
                }
            }
        });
    }

    // Theme select
    if ($(".theme_select")[0]) {
        $(".theme_select").click(
   			function() {
   			    if ($(".theme_select #ts_popup").queue().length == 0)
   			        $(".theme_select #ts_popup").slideToggle("fast");
   			}
		);
        if (document.all) {
            $(".theme_select").blur(
	   			function() {
	   			    setTimeout(function() { $(".theme_select #ts_popup").slideUp("fast"); }, 50);
	   			}
			);
        } else {
            $("body").click(
	   			function() {
	   			    if ($(".theme_select #ts_popup").queue("fx").length == 0) {
	   			        $(".theme_select #ts_popup").slideUp("fast");
	   			    }
	   			}
			);
        }
    }


    // Tabs on portfolio
    /*if ($(".content ul.ui-tabs-nav")[0]) {
        $(".content ul.ui-tabs-nav").tabs({ fx: { opacity: 'toggle' }, cache: false, spinner: '0% &hellip;' });
    }*/

    /*   
    // Search result text field
    if($(".resdesc p a")[0] && $(".resdesc p input")[0]) {
    $(".resdesc p a").click(
    function() {
    $(this).css("display", "none");
    $(this).siblings("input").css("display", "inline");
    $(this).siblings("input").focus();
    return false;
    }
    );
    $(".resdesc p input").blur(
    function() {
    $(this).css("display", "none");
    $(this).siblings("a").attr("innerHTML", $(this).attr("value"));
    $(this).siblings("a").css("display", "inline");
    }
    );
    }


	// Feedback text field
    if ($(".feedback p a")[0] && $(".feedback p input")[0]) {
    $(".feedback p a").click(
    function() {
    $(this).css("display", "none");
    $(this).siblings("input").css("display", "inline");
    $(this).siblings("input").focus();
    return false;
    }
    );
    $(".feedback p input").blur(
    function() {
    if ($(this).attr("value").replace(/\s+$/, '') != '')
    $(this).siblings("a").attr("innerHTML", $(this).attr("value"));

        	    $(this).css("display", "none");
    $(this).siblings("a").css("display", "inline");

   			}
    );
    }/**/

    // Two view text field
    if ($(".twoviewfield a")[0] && $(".twoviewfield input")[0]) {
        $(".twoviewfield a").click(
   			function() {
   			    $(this).css("display", "none");
   			    $(this).siblings("input").css("display", "inline");
   			    $(this).siblings("input").focus();
   			    return false;
   			}
   		);
        $(".twoviewfield input").blur(
   			function() {
   			    if ($(this).attr("value").replace(/\s+$/, '') != '')
   			        $(this).siblings("a").attr("innerHTML", $(this).attr("value"));

   			    $(this).css("display", "none");
   			    $(this).siblings("a").css("display", "inline");

   			}
   		);
    }

    // Arrows on work
    if ($(".top_portfolio_img")[0]) {
        $(".top_portfolio_img map area").hover(
   			function() {
   			    var cln = $(this).attr("className");
   			    $(".top_portfolio_img div a." + cln).stop();
   			    $(".top_portfolio_img div a." + cln).animate({ opacity: 1 }, 300);
   			},
   			function() {
   			    var cln = $(this).attr("className");
   			    if (!$(".top_portfolio_img div a." + cln).get(0).onthishover) {
   			        $(".top_portfolio_img div a." + cln).stop();
   			        $(".top_portfolio_img div a." + cln).animate({ opacity: 0 }, 300);
   			    }
   			}
   		);
        $(".top_portfolio_img div a").hover(
   	   		function() {
   	   		    this.onthishover = true;
   	   		    $(this).stop();
   	   		    $(this).animate({ opacity: 1 }, 300);
   	   		},
   	   		function() {
   	   		    this.onthishover = false;
   	   		    $(this).stop();
   	   		    $(this).animate({ opacity: 0 }, 300);
   	   		}
   		);
    } /**/


    // Partners
    if ($(".partners")[0]) {
        $(".partners a.oc").click(
			function() {
			    var p = $(this).siblings("p");
			    if (p.is(":hidden")) {
			        $(this).addClass("act");
			        p.slideDown();
			    } else {
			        $(this).removeClass("act");
			        p.slideUp();
			    }
			    return false;
			}
		);
    }

});

function initPortfolioSlider(id) {
    $("#" + id + " > li:not(.select) ul").hide();
    $("#" + id + " > li > a").click(
	    function() {
	        if ($("ul:hidden", $(this).parent()).get(0)) {
	            $("#" + id + " > li.select ul").slideUp("slow");
	            $("#" + id + " > li.select").attr("className", "");
	            if (this.parentNode.className == "select") this.parentNode.className = "";
	            else this.parentNode.className = "select";
	            $(this).siblings("ul").slideDown("slow");
	        }
	        return false;
	    }
    );
}

function initPortfolioClients() {
    if (isIE) {
        $(".by_clients a img").each(
		    function() {
		        var src = this.src;
		        this.src = "/files/images/blank.gif";
		        this.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
		    }
	    );
    }
}

function doTogleFromFlash(id, act) {
    id = "sifrel" + id;
    if (act) {
        $("#" + id).parent().siblings("ul").get(0).className = "";
        $("#" + id).parent().siblings("ul").slideUp("slow");
    } else {
        $("#" + id).parent().siblings("ul").get(0).className = "select";
        $("#" + id).parent().siblings("ul").slideDown("slow");
    }
}

function doSlide(id) {
    $(".main_menu")[0].bProcDo = true;
    $(".main_menu li ul:visible").slideUp("slow", function() { if ($(this).queue().length == 0) $(".main_menu")[0].bProcDo = false; });
    $(id).next().slideDown("slow", function() { if ($(this).queue().length == 0) $(".main_menu")[0].bProcDo = false; });
}

function checkTagName(obj, tag) {
    res = false;
    if (obj) {
        if (obj.tagName) {
            if (obj.tagName.toLowerCase() == tag.toLowerCase()) {
                res = true;
            }
        }
    }
    return res;
}

function changeTheme(url) {
    window.location = url;
    return false;
}




/**********************************************************/
/******************* VALIDATION ***************************/
/**********************************************************/

var validFields = new Array();

function addValidator(fid, types) {
    validFields[fid] = types;
    $("#" + fid).change(function() { isValidField(fid); });
}

function isValidField(fid) {
    hideError(fid);

    for (var t in validFields[fid]) {
        if (validFields[fid][t].indexOf("required") == 0 && $("#" + fid).get(0).value.length == 0) {
            displayError(fid, validFields[fid][t].substring(9));
            return false;
        }
        if (validFields[fid][t].indexOf("email") == 0) {
            var reg = /[0-9a-z_]+@[0-9a-z_^.]+\.[a-z]{2,3}/i;
            if (!reg.test($("#" + fid).attr("value"))) {
                displayError(fid, validFields[fid][t].substring(6));
                return false;
            }
        }
    }

    return true;
}

function isValid() {
    var valid = true;

    for (var fid in validFields)
        if (!isValidField(fid))
            valid = false;

    return valid;
}

function displayError(fid, err) {
    $("#" + fid).parent().after("<p class='err' id='"+fid+"_err'>" + err + "</p>");
}

function displayMessage(fid, msg) {
    $("#" + fid).parent().after("<p class='msg'>" + msg + "</p>");
}

function hideError(fid) {
    $("#" + fid).parent().parent().children().remove("#" + fid + "_err");
}

function hideErrors() {
    $(".content").children().remove("p.err");
}

function hideControl(fid) {
    $("#" + fid).parent().children().remove("#" + fid);
}
