// ON LOAD PAGE EVENT //
$(document).ready( function() {
	// ajax call logos
	//$('#logos_content').load('index.php?page=logotype&media=ajax&item=11');
	// check offset onload window event - show -hide button "na vrh"
	var YoffSet = getYOffset();
    checkOffsetToggle(YoffSet);
	tooltip();
	//setSWFilter();
	$.ajax({
		url: 'index.php?page=logotype&media=ajax&item=11',
		success: function(data) {
			if( data == '' ) {
				$('#logos').hide();
			} else {
				$('#logos_content').html( data );
			}
			if( LOGO_PAGES_N == 0 ) {
				$('#alogonext').hide();
				$('#alogoprev').hide();
			}
		},
		dataType: "html"
	});
	$('.ftp_submit').click(function(){
		var ftp_url = 'ftp://';
		ftp_url += $('#username').val()+':';
		ftp_url += $('#password').val()+"@izmenjava.emigma.com";
		window.open(ftp_url,'_blank');
		$('#login_form :input').not(':button').each(function(index){
			//ftp_url += $(this).val();
			
			//alert($(this).val());
		});
		
	});
	
	// Login form // Clear ko ni spremembe
	$('#login_form :input').not(':button').focus(function(){
		//$(this).attr('title',$(this).val());
		if($(this).attr('alt') == $(this).val()){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val($(this).attr('alt'))
		}
	});
	// news switcher
	if($("#news_slider") && $("#slider_box li").size() > 1){
		
		$("#news_slider").easySlider({
			//auto: true,
			//continuous: true,
			controlsBefore:	'<div id="news-controls">',
			controlsAfter:	'</div>',
			nextText: '>',
			prevText: '<',
			controlsFade:true,
			speed:800	
		});	
	}
	/* LOAD PAGES WITH `get_page` FUNCTION */
	get_page('vacant_job',7,'#job_desc');
	
	/*if($('#sliderr')){
		$('#sliderr').jshowoff({
			links: false,
			hoverPause: true,
			speed: 3000,
			autoPlay: true,
			effect: 'fade'
		});
	}*/
	
	var tmp_url = window.location.href;
	if( tmp_url.match( /#\/index/ ) ) {
		$.address.externalChange(function(e){
			$('#sw_content').load($.address.value());	
		});
	} else {
		$('#sw_content').load('index.php?page=project&media=ajax&item=2');
	}

	/*$.ajax({
		url: 'index.php?page=project&media=ajax&item=2',
		success: function(data) {
			$( '#sw_content' ).html( data );
		}
	});*/		
	
	$('a[href*=#]').click(function() {
		//document.title = $(this).attr("rel");
	 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
	 && location.hostname == this.hostname) {
	   var $target = $(this.hash);
	   $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
	   if ($target.length) {
	  var targetOffset = $target.offset().top;
	  //$('.na_vrh').text(targetOffset);
	  $('html,body').animate({scrollTop: targetOffset}, 1000);
    return false;
   }
 }
  });
 	
	$('#all_cb').change(function(){
	 	$("INPUT[type=checkbox]").attr('checked', $('#all_cb').is(':checked'));
		 swFilter();
	});
	$('INPUT[type=checkbox]').change(function(){
		var i = 0;
		var checkboxes_no = $('INPUT[type=checkbox]').length -1;
		//console.log(checkboxes_no);
		$('INPUT[type=checkbox]:checked').each(function(index) {
			if($(this).val() != 'all_cb'){
				i++;
			}
		 });
		 if(i < checkboxes_no){
			$('#all_cb').attr('checked',0) 
		}else{
			$('#all_cb').attr('checked',1) 
		}
	});
});
// SCROLL LISTENER cross browser
function getYOffset() {
    var pageY;
    if(typeof(window.pageYOffset)=='number') {
       pageY=window.pageYOffset;
    }
    else {
       pageY=document.documentElement.scrollTop;
    }
    return pageY;
}
// reshow form after data post
function reshowForm( formId ) {
	$.ajax({
		url: 'index.php?page=contact&item='+formId+'&media=ajax',
		type: 'get',
		success: function( data ) {
			$( '#div_'+formId ).html( data );
		}
	});
}
//Switching between jobs
var currentLogoPage = 0;
function switchToJob( selectElem ) {
	var sel = selectElem.options[selectElem.selectedIndex].value;
	for( var i = 0; i < selectElem.children.length; i++ ) {
		var s =  selectElem.options[i].value;
		if( $( '#job_li_'+s ).is( ':visible' ) ) {
			$( '#job_li_'+s ).fadeOut( 'fast', function() {
				$( '#job_li_'+sel ).fadeIn( 'fast' );
			} );
			break;
		}
	}
}

// funkcije za menjavanje logotipov
function logosNext( pagesTotal ) {
	if( currentLogoPage < pagesTotal) {
		currentLogoPage++;
		var limit = currentLogoPage;
		$.ajax({
			url: 'index.php?page=logotype&media=ajax&item=11&limit='+limit,
			success: function( data ) {
				$('#logos_content').fadeOut( 'slow', function() {
					$('#logos_content').html( data ).fadeIn( 'slow' );
				} );
			}
		});
	}
	if(currentLogoPage == pagesTotal) {
		$('#alogonext' ).hide();
	}
	if(currentLogoPage != 0) {
		$('#alogoprev').show();
	}
}



function logosPrev(pagesTotal) {
	if( currentLogoPage > 0 ) {
		currentLogoPage--;
		var limit = currentLogoPage;
		$.ajax({
			url: 'index.php?page=logotype&media=ajax&item=11&limit='+limit,
			success: function( data ) {
				$('#logos_content').fadeOut( 'slow', function() {
					$('#logos_content').html( data ).fadeIn( 'slow' );
				} );
			}
		});
	}
	if( currentLogoPage == 0 ) {
		$('#alogoprev').hide();
	}
	$( '#alogonext' ).show();
}
// konec funkcije za menjavanje logotipov

function get_page(page,item,jq_selector){
	if(page || item || jq_selector){
		$.ajax({
			url: 'index.php?page='+page+'&media=ajax&item='+item,
			success: function(data) {
				$(jq_selector).html(data);
			}
		});	
	}else{
		alert('check parameters');
	}
}

// funkcije za menjavanje projektov
var currentSWPage = 0;
function getSWFilter() {
	var f = '&filter=';
	$('INPUT[type=checkbox]:checked').each(function(index) {
		if($(this).val() != 'all_cb'){
			f += $(this).val() + '|';
		}
	});
	if( f == '&filter=' ) return '';
	else return f.substring( 0, f.length -1 );
}

function setSWFilter() {
	var f = '&filter=';
	$('.sort_checkbox input[type=checkbox]').each(function(i,value){
		if($(this).val() != 'all_cb'){		
			f += $(this).val() + '|';
		};
		$(this).attr('checked',1);
	});
	return f.substring( 0, f.length -1 );
	//console.log("FILTER:"+f.substring( 0, f.length -1 ));	
}

function getSWSort() {
	var v = $( '#swSortId' ).val();
	if( v == 'empty' ) return '';
	else return '&sort='+v;
}

function swFilter() {
	var p = '';
	if( $('#partnersDD' ).val() != -1 ) {
		PID = $('#partnersDD' ).val();
		p = '&pid='+PID;
	}
	currentSWPage = 0;
	$.ajax({
		url: 'index.php?page=project&item=2&media=ajax'+getSWFilter()+getSWSort()+p,
		success: function(data) {
			$('#sw_content' ).fadeOut('slow', function() {
				$(this).html( data ).fadeIn('slow');
			});
		}
	});
}

function swSort() {
	swFilter();
}

function svNext() {
	currentSWPage++;
	$.address.value('index.php?page=project&item=2&media=ajax'+getSWFilter()+getSWSort()+'&limit='+currentSWPage ); 
	if( PID != null ) var urlApp = '&pid='+PID;
	else var urlApp = '';
	var jqxhr = $.ajax({
		url: 'index.php?page=project&item=2&media=ajax'+getSWFilter()+getSWSort()+'&limit='+currentSWPage+urlApp,
		success: function( data ) {
			if( data == '' ) {
				if( $( '#sw_message' ).length == 0 ) {
					$( '.project_container' ).after( '<div align="center" id="sw_message">'+END_OF_LIST+'</div>' );
				}/* else {
					$( '#sw_message' ).effect( 'shake', {times:3}, 200 );
				}*/
				currentSWPage--;
			} else {
				$( '#sw_content' ).fadeOut('slow', function() {
					$(this).html( data ).fadeIn( 'slow' );
					$( '#sw_message' ).hide('fast');
				} );
			}
		}
	});
}
function clearClient() {
	PID = null;
	currentSWPage = 0;
}

function svPrev() {
	var limit = currentSWPage;
	$.address.value( 'index.php?page=project&item=2&media=ajax'+getSWFilter()+getSWSort()+'&limit='+currentSWPage ); 
	if( currentSWPage > 0 ) currentSWPage--;
	if( PID != null ) var urlApp = '&pid='+PID;
	else var urlApp = '';
	$.ajax({
		url: 'index.php?page=project&item=2&media=ajax'+getSWFilter()+getSWSort()+'&limit='+currentSWPage+urlApp,
		success: function( data ) {
		if( limit == 0 ) {
				if( $( '#sw_message' ).length == 0 ) {
					$( '.project_container' ).after( '<div align="center" id="sw_message">'+END_OF_LIST+'</div>' );
				} /*else {
					$('#sw_message').effect( 'shake', {times:3}, 200 );
				}*/
			} else {
				$( '#sw_content' ).fadeOut('slow', function() {
					$(this).html( data ).fadeIn( 'slow' );
					$('#sw_message').hide('fast');
				} );
			}
		}
	});

}
// konec funkcije za menjavanje projektov

function scrollToSW(filter) {
	$.each($('#services_and_work input'), function() {
		var re = new RegExp( filter );
		if( this.id.match( re ) ) {
			this.checked = 'checked';
		} else {
			this.checked = '';
		}
	});
	swFilter();
	//window.location.href = '#serwork';
}


/* FONT SIZER FUNCTION - javascript */
var tags = new Array( 'div','td','tr','p','b','table','strong','span','emphasis','a','h1','h2','h3','pre','sub','sup','i','th','cp','ul','ol','li','dt','dd');
//var pixelArray =  new Array('9','10','11','13','15','17','19','21');
var pixelArray =  new Array('10','11','12','14','16','18','20','22');
var emArray =  new Array('0.7','0.9','1.0','1.5','2.0','2.5','3');
var initSize = parseInt(getCookie("fontsize"));
var PID = null;
//var cssSizes = new Object();

function getInitSize() {
	/*var rules;
	var i;
	if (document.styleSheets[0].cssRules) {
		rules = document.styleSheets[0].cssRules;
		i = 0;
	} else {
		rules = document.styleSheets[0].rules;
		i = -1;
	}

	while (++i < rules.length) {
		if (rules[i].style.fontSize.length > 0) {
			cssSizes[rules[i].selectorText] = rules[i].style.fontSize;
		}
	}
	*/

	if (!initSize && initSize != 0) { 
		initSize = 2;
		var now = new Date();
		now.setTime(now.getTime() + 2 * 24 * 60 * 60 * 1000);
		setCookie("fontsize", initSize, now);
	} else if (initSize != 2) {
		tmp = initSize-2;
		initSize = 2;
		fontSizer(tmp, 'px');
	}
}

function fontSizer(inc,unit) {
		
	if (!document.getElementById) 
		return;
	var size = initSize;
		size += inc;
	if (size < 3 ) {
		size = 2;
	}
	if (size > 5 ) {
		size = 5;
	}
		var diff = size - initSize;
		var pxdiff = diff;
		if (diff == 0) return;
		
		if (size >= 2 && initSize >= 2) {
			pxdiff *= 2;
		} else if (size >= 2) {
			pxdiff += size - 2;
		} else if (initSize >= 2) {
			pxdiff -= initSize - 2;
		}
		
		initSize = size;
		var now = new Date();
		now.setTime(now.getTime() + 2 * 24 * 60 * 60 * 1000);
		setCookie("fontsize", initSize, now);
		getBody = document.getElementsByTagName('body')[0];
	for (i = 0 ; i < tags.length ; i++ ) {
		getallTags = getBody.getElementsByTagName(tags[i]);
		for (k = 0 ; k < getallTags.length ; k++) {
			if (getallTags[k].className == 'left' || getallTags[k].className == 'content') {
					if (getallTags[k].style.fontSize.length > 0) {
						px = toInt(getallTags[k].style.fontSize);
						if (px > 0) {
							getallTags[k].style.fontSize = (unit=='px') ? (px+pxdiff)+unit : (emArray[size]*1 + px-emArray[size-diff])+unit;
						} else {
							getallTags[k].style.fontSize = (unit=='px') ? (pixelArray[size]*1 + px-pixelArray[size-diff])+unit : (emArray[size]*1 + px-emArray[size-diff])+unit;
						}
					/*} else if (cssSizes['.'+getallTags[k].style.className]) {
						px = toInt(cssSizes['.'+getallTags[k].style.className]);
						getallTags[k].style.fontSize = (unit=='px') ? (pixelArray[size]*1 + px-pixelArray[size-diff])+unit : (emArray[size]*1 + px-emArray[size-diff])+unit;
					*/
					} else {
						//alert(getallTags[k].style.className);
						getallTags[k].style.fontSize = (unit=='px') ? pixelArray[size]+unit : emArray[size]+unit;
					}
				
			}
		}
	}
}

function toInt(str) {
	for (j = 0; j <= str.length; j++) {
		if (j == str.length || str.charAt(j) < '0' || str.charAt(j) > '9') {
			return str.substring(0,j)*1;
		}
	}
}

/////////////////////

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
// Tooltip function for team app
this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
	/* END CONFIG */		
	$("img.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";	
		//alert(this.t);								  
		$("body").append("<div id='tooltip'>"+ this.t +"</div>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("img.tooltip").mousemove(function(e){
		$("#tooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");
	});			
};

// show - hide button "na vrh strani"
function checkOffset(offSet){
	if($.browser.msie){
		if(offSet < 180){
			 $('.to_the_top').hide();
		}else{
			 $('.to_the_top').show();
		}
	}else{
		if(offSet < 180){
			 $('.to_the_top').fadeOut();
		}else{
			 $('.to_the_top').fadeIn();
		}
	};

}
function checkOffsetFade(offSet){
	if(offSet < 180){
		 $('.to_the_top').fadeOut('slow');
	}else{
		 $('.to_the_top').fadeIn('slow');
	}
}	
function checkOffsetToggle(offSet){
	if(offSet < 180){
		 $('.to_the_top').hide();
	}else{
		 $('.to_the_top').show();
	}
}	
function showPartnersProjects( pID ) {
	PID = pID;
	
	currentSWPage = 0;
	setSWFilter();
	showPartnersRelatedStuff2(pID);
	$.ajax({
		url: 'index.php?page=project&item=2&media=ajax'+getSWFilter()+getSWSort()+'&limit=0&pid='+pID,
		success: function( data ) {
				
				$( '#sw_content' ).fadeOut('slow', function() {
					$(this).html( data ).fadeIn( 'slow' );
					$('#sw_message').hide('fast');
					$('#swID').click();
				} );
			}
	});
}

function showPartnersProjectsDD() { 
	var v = $( '#partnersDD' ).val();
	if( v == -1 ) {
		setSWFilter();
		clearClient();
		$.ajax({
		url: 'index.php?page=project&item=2&media=ajax'+getSWFilter()+getSWSort()+'&limit=0',
		success: function( data ) {
				$( '#sw_content' ).fadeOut('slow', function() {
					$(this).html( data ).fadeIn( 'slow' );
					$('#sw_message').hide('fast');
					
				} );
			}
	});
	} else {
		showPartnersProjects( v );
	}
}

// scroller listener 
$(window).scroll(function(e) {
	var offSet = getYOffset();
	if($.browser.msie){
		checkOffsetToggle(offSet);
	}else{
		checkOffsetFade(offSet);
	}
	
});
function showPartnersRelatedStuff2( projectID ) {
	$('#partnersDD option').each(function(i){
		if($(this).val() == projectID){
			$(this).attr('selected',1);
			//showPartnersProjectsDD()
		}
	});
//	alert( project_partner );
}
function showPartnersRelatedStuff( project_partner ) {
	$('#partnersDD option').each(function(i){
		if($(this).text() == project_partner){
			$(this).attr('selected',1);
			showPartnersProjectsDD()
		}
	});
//	alert( project_partner );
}

