server_name=window.location.host.split(":")[0];server_name='http://'+server_name;
var HTTP_HOST = 'http://' + window.location.hostname + '/';
    $(document).ready(function(){
        //Examples of how to assign the ColorBox event to elements
        $("a[rel='colorbox']").colorbox({maxHeight: '100%', scalePhotos: true, transition: 'fade'});
        $(document).bind('cbox_complete', function(){ 
            $('.cboxPhoto').unbind().click($.fn.colorbox.close); 
        }); 
        $(".send_question").colorbox({width:"50%", inline:true, href:"#send_question_inline"});
    });
    this.screenshotPreview = function(){
		xOffset = 150;
		yOffset = 30;
	$("a.screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='" + HTTP_HOST + "/images/prods/_thumb_" + this.rel + "' alt='url preview' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

version = navigator.appVersion.toLowerCase();
ie = (version.indexOf('msie')>-1);
subsVisible = [];
timer = false;
timer2 = false;
milisec = 500;

function subShow(subId) {
    list = document.getElementById('navi-list');
    submenus = list.getElementsByTagName('ul');
    for(i in submenus) {
        if(submenus[i].id) {
            if(submenus[i].id == subId) {
                elm = document.getElementById(subId);
                if(timer) {
                    clearTimeout(timer);
                    clearTimeout(timer2);
                }
                if(elm && !subsVisible[subId]) {
                    elm.style.display = "block";
                    subsVisible[subId] = true;
                    selectsVisibility('hidden');
                }
            } else {
                subHide(submenus[i].id);
            }
        }
    }
}

function subWannaHide(subId) {
    elm = document.getElementById(subId);
    if(elm && subsVisible[subId]) {
        timer = setTimeout("subHide(\""+subId+"\")", milisec);
        timer2 = setTimeout("selectsVisibility('visible')", milisec);
    }
}

function subHide(subId) {
    elm = document.getElementById(subId);
    if(elm && subsVisible[subId]) {
        elm.style.display = "none";
        subsVisible[subId] = false;
    }
}

function menuClick(parentid) {
    listid = 'menu-parentid-'+parentid;
    buttonid = 'a-parentid-'+parentid;
    filterid = 'filter-parentid-'+parentid;
        
    list = document.getElementById(listid);
    button = document.getElementById(buttonid);
    filter = document.getElementById(filterid);

    if(button.className == 'tree closed') {
        button.className = 'tree open'
        if(list) {
            list.style.display = "block";
        }
        if(filter) {
            filter.style.display = "block";
        }
    } else {
        button.className = 'tree closed'
        if(list) {
            list.style.display = "none";
        }
        if(filter) {
            filter.style.display = "none";
        }
    }
}

// starting the script on page load
$(document).ready(function(){
	screenshotPreview();
});

// JavaScript Document

// DropDownMenu by Miha Hribar
// http://hribar.info

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function showResult(str) {
	if (str.length==0) {
		document.getElementById("sb-results-body").innerHTML="";
		$('#site-search').removeClass("activated");
		return;
	}
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
			document.getElementById("sb-results-body").innerHTML=xmlhttp.responseText;
			$('#site-search').addClass("activated");
		}
	}
	
	xmlhttp.open("GET",server_name+"/search_ajax.php?search="+str,true);
	xmlhttp.send();
}

function prepareMenu() {
    // first lets make sure the browser understands the DOM methods we will be using
  	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	
  	// lets make sure the element exists
  	if (!document.getElementById("menu_js")) return false;
  	var menu = document.getElementById("menu_js");
  	
  	// for each of the li on the root level check if the element has any children
  	// if so append a function that makes the element appear when hovered over
  	var root_li = menu.getElementsByTagName("li");
  	for (var i = 0; i < root_li.length; i++) {
  	    var li = root_li[i];
  	    // search for children
  	    var child_ul = li.getElementsByTagName("ul");
  	    if (child_ul.length >= 1) {
  	        // we have children - append hover function to the parent
  	        li.onmouseover = function () {
  	            if (!this.getElementsByTagName("ul")) return false;
  	            var ul = this.getElementsByTagName("ul");
  	            ul[0].style.display = "block";
  	            return true;
  	        }
  	        li.onmouseout = function () {
  	            if (!this.getElementsByTagName("ul")) return false;
  	            var ul = this.getElementsByTagName("ul");
  	            ul[0].style.display = "none";
  	            return true;
  	        }
  	    }
  	}
  	
  	return true;
}

addLoadEvent(prepareMenu);
