function write_swf(filename,id,width,height,extra) {
	document.write('<object type="application/x-shockwave-flash" data="'+filename+(id!=null?'?cid='+id:'')+'" width="'+width+'" height="'+height+'">');
	document.write('<param name="movie" value="'+filename+(id!=null?'?cid='+id:'')+'" />');
	
	document.write('<param name="quality" value="autohigh" />');
	document.write('<param name="wmode" value="transparent" />');
	document.write('<param name="menu" value="false" />');
	

	for (i in extra) {
			document.write('<param name="'+extra[i][0]+'" value="'+extra[i][1]+'" />');
	}
	document.write('</object>');
}


function write_flash(filename, width, height, imagepath, paramstr) {
	
	
		document.write("<script type='text/javascript'>");
		document.write("AC_FL_RunContent(");
		document.write("'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',");
		document.write("'width', "+width+",");
		document.write("'height', "+height+",");
		document.write("'src', 'flash/"+filename+"',");
		document.write("'quality', 'high',");
		document.write("'pluginspage', 'http://www.macromedia.com/go/getflashplayer',");
		document.write("'align', 'middle',");
		document.write("'play', 'true',");
		document.write("'loop', 'true',");
		document.write("'scale', 'showall',");
		document.write("'wmode', 'transparent',");
		document.write("'devicefont', 'false',");
		document.write("'id', '"+filename+"',");
		document.write("'bgcolor', 'transparent',");
		document.write("'name', '"+filename+"',");
		document.write("'menu', 'true',");
		document.write("'allowFullScreen', 'false',");
		document.write("'allowScriptAccess','sameDomain',");
		document.write("'movie', 'flash/"+filename+"',");
		document.write("'flashvars', '"+paramstr+"',");
		document.write("'salign', ''");
		document.write(");");
		document.write("</script>");
	

	
}

function show_video_file(videofile,previewfile,sizex,sizey,autoplay,fullscr)
{
	auto_play = false;
	full_screen = true;
	if (autoplay != null){
		auto_play = true;
	}
	if (fullscr != null){
		full_screen = true;
	}
	var vars = {
		width:sizex,
		height:sizey,
		file:videofile,
		image:previewfile,
		fullscreen:full_screen,
		javascriptid:'video',
		enablejs:'true',
		backcolor:'888888',
		frontcolor:'FFFFFF',
		lightcolor:'FFFFFF',
		screencolor:'000000',
		autostart:auto_play,
		controlbar:'over'
	};
	
	var params = {
		allowscriptaccess:'always',
		allowfullscreen:full_screen,
		wmode:'transparent'
	};
	
	swfobject.embedSWF("flash/player.swf", "video", sizex, sizey, "9.0.0",'', vars, params);
}

function show_video_file_by_id(id,videofile,previewfile,sizex,sizey,autoplay,fullscr)
{
	auto_play = false;
	full_screen = true;
	if (autoplay != null){
		auto_play = true;
	}
	if (fullscr != null){
		full_screen = true;
	}
	var vars = {
		width:sizex,
		height:sizey,
		file:videofile,
		image:previewfile,
		fullscreen:full_screen,
		javascriptid:id,
		enablejs:'true',
		backcolor:'888888',
		frontcolor:'FFFFFF',
		lightcolor:'FFFFFF',
		screencolor:'000000',
		autostart:auto_play,
		controlbar:'over'
	};
	
	var params = {
		allowscriptaccess:'always',
		allowfullscreen:full_screen,
		wmode:'transparent'
	};
	
	swfobject.embedSWF("flash/player.swf", id, sizex, sizey, "9.0.0",'', vars, params);
}

function changeHTML(objectid,newtext){
	document.getElementById(objectid).innerHTML = newtext;
}

/*
* PROTOTYPES for IE and other older browsers
*/
// in array function
function inArray(needle, haystack) {
	for (h in haystack) {
		if (haystack[h] == needle) {
			return true;
		}
	}
	return false;
}
// indexOf function
if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function(elt /*, from*/) {
    var len = this.length;
    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++) {
      if (from in this && this[from] === elt)
	  return from;
    }
    return -1;
  };
} 

/* toggle font / line size */
/* set cookie */
//function setCookie(name, value, expires, path, domain, secure) {
function setCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

/* get cookie */
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));
}

/* delete cookie */
function deleteCookie( name, path, domain ) {
	if ( getCookie(name) ) 
		document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

var text_sizes		= [12, 14, 16];
var line_heights	= [1.4, 1.6, 1.8];

var def_text_size = 0;
var def_line_height = 0;

var current_lh = false;
var current_ts = false;

var first_page = false;

var content_placeholder = 'middleblock';

function change_line_height() {

	if (!current_lh)
	current_lh = getCookie('line_height');

	if (!current_lh) current_lh = def_line_height;
	current_lh ++;
	if (current_lh == line_heights.length) current_lh = 0;

	setCookie('line_height', current_lh, 365);
	if (first_page) {
		document.getElementById('push_news').style.lineHeight = line_heights[current_lh] + "em";
		document.getElementById('push_strokovnjak').style.lineHeight = line_heights[current_lh] + "em";
	} else {
		document.getElementById('middleblock').style.lineHeight = line_heights[current_lh] + "em";
	}
}

function change_text_size() {
	if (!current_ts)
		current_ts = getCookie('text_size');
	if (!current_ts) current_ts = def_text_size;
	current_ts ++;
	if (current_ts == text_sizes.length) current_ts = 0;
	setCookie('text_size', current_ts, 365);
	if (first_page) {
		document.getElementById('push_news').style.fontSize = text_sizes[current_ts] + "px";
		document.getElementById('push_strokovnjak').style.fontSize = text_sizes[current_ts] + "px";
	} else {
		document.getElementById('middleblock').style.fontSize = text_sizes[current_ts] + "px";
	}
}

/* returns the x,y coordinates of the current screen (for multi monitor setup) */
function get_top_left(width, height){
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
	  		winW = window.innerWidth;
	  		winH = window.innerHeight;
	 		winL = window.screenX;
	 		winT = window.screenY;
	 	} else {
	  		winW = document.body.offsetWidth;
	  		winH = document.body.offsetHeight;
	  		winL = window.screenLeft;
	  		winT = window.screenTop;
	 	}
	}
	
	var scroll = 0;
	var l = winL + winW/2 - width/2;
	var t = winT + winH/2 - height/2;
	
	
  	if(screen.width <= width){
  		width=screen.width;
  	}
  	if(screen.height <= height){
  		height=screen.height;
 	 	var t = 0;
  		var scroll = 1;
  	}
	return Array(t,l);
}


/* generic window opener */
function openwin(uri,title,w,h,scroll,tracking){
	
	if (tracking == undefined) tracking = false;
	
	if (tracking) {
		pageTracker._trackEvent('heaeder', 'Click', 'Radio popup');
	}
	
	if(w){wwidth = w;}else{	wwidth = 380;}
	if(h){wheight = h; }else{ wheight = 380;}
	xy = get_top_left(wwidth, wheight);
	wleft = xy[1];
	wtop = xy[0];
	wnd = window.open(uri, title, "width="+wwidth+", height="+wheight+", left="+wleft+", top="+wtop+", toolbar=no, location=no, scrollbars="+scroll+", status=no, menubar=no, resizable=no, directories=no");
}



function open_popover() {
	$(this).modal({width:600, height:440}).open();
}
function open_popover_trip() {
	//$.ceebox.show('', 'popover_video.php', false);
	href = TRIP_FLASH_HREF;
	$(this).modal({width:640, height:352, src:href}).open();
}

function open_popover_stream() {
	//$.ceebox.show('', 'popover_video.php', false);
	href = "/popover_stream.php";
	$(this).modal({width:960, height:720, src:href}).open();
}

$(function() {
	if ($('.ad-gallery').size()>0) {
		var galleries = $('.ad-gallery').adGallery();
	}
});

function show_gallery_item (galID,itemID) {
	$(galID+" span").css("display","none");
	$(".izbirnik .item a").removeClass("active");
	$("#bimg_"+itemID).css("display","block");
	$("#th_"+itemID).addClass("active");
	return false;
}

function remove_all_from_comparator() {
	
	$.ajax({
		url:'ajax.request.php?action=remove_all_from_comparator',
		type:'POST',
		dataType:'html',
		success: function(rsp) {
			$('#box-preview-comparator-top').html(rsp);
			$('#box-preview-comparator-bottom').html(rsp);
		}
	});
	
	$(".in-comparator").each(function(){
		$(this).removeClass("in-comparator generic-primary-border");
		$(this).children("a.toggle-comparator").addClass("comparator-add");
		$(this).children("a.toggle-comparator").removeClass("comparator-remove generic-primary-background");
		$(this).children("a.toggle-comparator").html('<span class="fs14">&rsaquo;</span>&nbsp;Dodaj v primerjalnik');
		
	});
	
}

function remove_from_comparator(itemID) {
	
	$.ajax({
		url:'ajax.request.php?action=remove_from_comparator',
		type:'POST',
		dataType:'html',
		data: {ID:	itemID},
		success: function(rsp) {
			$('#box-preview-comparator-top').html(rsp);
			$('#box-preview-comparator-bottom').html(rsp);
		}
	});
	
	if ($("#add-to-comparator").length > 0) {
		$("#add-to-comparator").html('<a href="#" class="pngfix" onclick="add_to_comparator('+itemID+'); return false;">Dodaj v primerjalnik</a>');
	}
	
	if ($("#katalog-list").length > 0) {
	
		$("#telefon_"+itemID).removeClass("in-comparator generic-primary-border");
		$("#telefon_"+itemID).children("a.toggle-comparator").addClass("comparator-add");
		$("#telefon_"+itemID).children("a.toggle-comparator").removeClass("comparator-remove generic-primary-background");
		$("#telefon_"+itemID).children("a.toggle-comparator").html('<span class="fs14">&rsaquo;</span>&nbsp;Dodaj v primerjalnik');
	
	}
	
	if ($("#comparator").length > 0) {
		
		$("#item"+itemID).addClass("hidden");
		set_telephone_slider();
		
	}
}

function add_to_comparator(itemID) {
	
	$.ajax({
		url:'ajax.request.php?action=add_to_comparator',
		type:'POST',
		dataType:'html',
		data: {ID:	itemID},
		success: function(rsp) {
			$('#box-preview-comparator-bottom').html(rsp);
		}
	});
	
	$("#add-to-comparator").html('<a href="#" class="pngfix" onclick="remove_from_comparator('+itemID+'); return false;">Odstrani iz primerjalnika</a>');
	
}

function set_telephone_slider() {
	if ($('#comparator').length > 0) {
		
		var sliding_width = 252;
		var num_elements = $("#comparator .item").length;
		$("#comparator .item").each(function(){
			if ($(this).hasClass("hidden")) {
				num_elements--;
			}
		});
		
		if (num_elements == 0) {
			$("#comparator .no-telephones").removeClass("hidden");
		} else {
			$("#comparator .no-telephones").addClass("hidden");
		}
		
		var holder_width = num_elements * 252;
		var holder_position = parseInt($("#comparator-slider").css("left"));
		
		$("#comparator-slider").css( 'width', holder_width);
		
		if (holder_position == 0) $("#slide-back").css("display","none");
		if (num_elements < 4) {
			$("#slide-forward").css("display","none");
			$("#slider-controls").css("display","none");
			$("#comparator-slider").css( 'left','0');
		} else {
			$("#slide-forward").css("display","block");
		}
		
		$("#slide-forward").click(function(){
		
			holder_position = holder_position - sliding_width;
			$("#comparator-slider").animate({left: holder_position+'-'+sliding_width+'px'},'slow');
		
			if (holder_position < 0) $("#slide-back").css("display","block");
			if (holder_position == -holder_width + (sliding_width*3)) $(this).css("display","none");
			
			return false;
		});
		
		$("#slide-back").click(function(){
		
			holder_position = holder_position + sliding_width;
			$("#comparator-slider").animate({left: holder_position+'+'+sliding_width+'px'},'slow');
		
			if (holder_position == 0) $(this).css("display","none");
			if (holder_position > -holder_width + sliding_width) $("#slide-forward").css("display","block");
			
			return false;
		});
	}
}


function filter_telephones(){
	
	$("#katalog-list").html("");
	$("#katalog-list").css({
		backgroundImage:'url(images/loading_black.gif)',
		backgroundPosition:'center center',
		backgroundRepeat: 'no-repeat',
		height: '400px'
	});
	
	$.ajax({
		url:'ajax.request.php?action=filter_telefones',
		type:'POST',
		dataType:'html',
		success: function(rsp) {
			$("#katalog-list").css({
				background:'none',
				height: '1%'
			});
			$("#katalog-list").html(rsp);
	}
	});
	
}

$(document).ready(function(){
	
	set_telephone_slider();
	
	$('a','.box-social-bookmarks').click(function(){
		var popupUrl = $(this).attr('href');
		openwin(popupUrl,'',500,600,'yes');
		return false;
	});
	
	$('.popup-pogoji-href').click(function(){
		var popupUrl = $(this).attr('href');
		//var popupTitle = $('.popup-pogoji-href span').text();
		
		openwin(popupUrl,'',500,600,'yes');
		return false;
	});
	
	if ($("#katalog-filter").length > 0) {
		
		$('.fake-checkbox').each(function(){
			$(this).addClass("checkbox");
			$(this).click(function(){ 
				
				$(this).toggleClass("checked");
				$(this).parent().toggleClass("checked");
				
				var input = $("input",this);
				
				if (input.attr("checked")) {
					input.removeAttr("checked");
				} else {
					input.attr("checked","checked");
				}
				
				id = $(this).children("input").attr("id");
				
				$.ajax({
					url:'ajax.request.php?action=add_to_session',
					type:'POST',
					dataType:'html',
					data: {VAL:	id},
					success: function() {}
				});
				
				return false;
			});
		});
		/*
		$('.fake-checkbox label').click(function(){
			//$(this).click(function(){ $(this).parent().toggleClass("checked"); });
			$("input",$(this).closest("span")).trigger("click");
		});
		*/
		
	}
	
	$("#show-podrobnosti").click(function(){
		$("#detail-view .podrobnosti").toggleClass("hidden");
		return false;
	});
	
	if ($("#katalog-list").length > 0) {
		
		$(".detail-link").each(function() {
			
			$(this).mouseover(function(){ $(this).parent().children("h4").addClass("generic-primary-color"); });
			$(this).mouseout(function(){ $(this).parent().children("h4").removeClass("generic-primary-color"); });
			
		});
		
		
		$(".toggle-comparator").each(function(){
			
			$(this).click(function(){
				
				var itemID = $(this).attr("href");
				
				if ($(this).hasClass("comparator-add")){
					
					$.ajax({
						url:'ajax.request.php?action=add_to_comparator',
						type:'POST',
						dataType:'html',
						data: {ID:	itemID},
						success: function(rsp) {
							$('#box-preview-comparator-top').html(rsp);
							$('#box-preview-comparator-bottom').html(rsp);
						}
					});
					
					$(this).parent().addClass("in-comparator generic-primary-border");
					$(this).removeClass("comparator-add");
					$(this).addClass("comparator-remove generic-primary-background");
					$(this).html('<span class="fs14">&rsaquo;</span>&nbsp;Odstrani iz primerjalnika');
				
				} else {
					
					$.ajax({
						url:'ajax.request.php?action=remove_from_comparator',
						type:'POST',
						dataType:'html',
						data: {ID:	itemID},
						success: function(rsp) {
							$('#box-preview-comparator-top').html(rsp);
							$('#box-preview-comparator-bottom').html(rsp);
						}
					});
					
					$(this).parent().removeClass("in-comparator generic-primary-border");
					$(this).addClass("comparator-add");
					$(this).removeClass("comparator-remove generic-primary-background");
					$(this).html('<span class="fs14">&rsaquo;</span>&nbsp;Dodaj v primerjalnik');
				
				}
				
				
				return false;
				
			});
			
		});
		
		
		
	}
	
	if ($("#upgrades-table").length>0) {
		
		$("#upgrades-table a.option-cell").each(function(){
			
			var option_id = ('#option_'+$(this).attr("rel"));
			
			$(this).mouseover(function(){
				if (!$(this).parent().hasClass("generic-primary-background")){
					$(this).addClass("high-lighted");
				}
				$(this).parent().parent().children(".paket-cell").addClass("high-lighted");
				$(option_id).addClass("high-lighted");
			});
			$(this).mouseout(function(){
				$(this).removeClass("high-lighted");
				$(this).parent().parent().children(".paket-cell").removeClass("high-lighted");
				$(option_id).removeClass("high-lighted");
			});
			$(this).click(function(){
				
				var id = $(this).attr("href");
				
				$.ajax({
					url:'ajax.request.php?action=set_upgrade_info',
					type:'POST',
					dataType:'html',
					data: {ID:	id},
					success: function(rsp) {
						$('#upgrades-info').html(rsp);
					}
				});
				
				$("#upgrades-table div").each(function(){ $(this).removeClass("selected generic-primary-background"); });
				$(this).parent().parent().children(".paket-cell").addClass("selected");
				$(this).parent().addClass("generic-primary-background");
				$(this).removeClass("high-lighted");
				$(option_id).addClass("selected");
				
				
				return false;
			});
			
		});
		
	}
	
});
