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 return_swf(filename,id,width,height,extra) {
	var html = '';
	
	html += '<object type="application/x-shockwave-flash" data="'+filename+(id!=null?'?cid='+id:'')+'" width="'+width+'" height="'+height+'">';
	html += '<param name="movie" value="'+filename+(id!=null?'?cid='+id:'')+'" />';
	
	html += '<param name="quality" value="autohigh" />';
	html += '<param name="wmode" value="transparent" />';
	html += '<param name="menu" value="false" />';
	

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

/*
* 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){
	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=yes, status=no, menubar=no, resizable=no, directories=no");
}

/* main menu */
$(document).ready(function() {
	var d = new Date();
	d = new Date(d.setDate(d.getDate() + 14));
	var comparePhoneCookieOpts = {
		path:'/',
		expires: d
	};
	
	$('#main-menu li.closed').each(
	function(){
		if (!($(this).hasClass('selected'))) {
			$(this).removeClass('over');
			$('#main-menu li.selected a').css('backgroundPosition','0 0px');
			$('#main-menu li.selected a span').css('backgroundPosition','right 0px');
			$('ul',this).removeClass('open');
			$('ul.selected').addClass('open');
			$('#main-menu').removeClass('sub-open');
		}
		
	});
	
	$('#main-menu li ul *').hover(function(e){
		e.stopPropagation();
	});
	
	if (!$('.ajax').parents('li').hasClass('selected'))
		$('.ajax:first').parents('li').addClass('selected');
	
	$('.ajax').click(function(){
		
		$('.ajax').parents('li').removeClass('selected');
		$(this).parents('li').addClass('selected');
		$(this).blur();
		
		var url = $(this).attr('href') + '?ajax=' + $(this).attr('rel');
		var loadIn = $('.load-content',$('.dynamic-content'));
		loadIn.load(url);
		
		return false;
	});

	$('.add-to-phone-compare').live('click' ,function(e) {
		var id = $(this).attr('data-id');
		var ids = $.cookie('phone_compare');
		if (ids) {
			ids = ids.replace(/\s+/, '').split(/,/);
		} else {
			ids = [];
		}

		if ($.inArray(id, ids)==-1) {
			if (ids.length<3) {
				nope = false;
				ids.push(id);

				var title = $(this).attr('data-title');
				$('#ozji_izbor .phones').append(
					'<span class="phone" data-id="'+id+'">'+
						title+
					'</span>'
				);


				ids = ids.join(',');
				$.cookie('phone_compare', ids, comparePhoneCookieOpts);

				$('.add-to-phone-compare[data-id="'+id+'"]').addClass('selected');
			} else {
				alert("Primerjava je možna med največ tremi telefoni.");
			}
		} else {
			alert("Ta telefon je že v primerjalniku.");
		}
		
		e.preventDefault();
	});

	$('#ozji_izbor').click(function(e) {
		if (!$(e.target).is('.phone')) {
			var url = $(this).attr('data-url');
			if (url.indexOf('?')==-1) {
				url += '?';
			} else {
				url += '&';
			}

			var ids = $.cookie('phone_compare');
			if (ids) {
				ids = ids.replace(/\s+/, '').split(/,/);
			} else {
				ids = [];
			}

			if (ids.length>0) {
				for (var i=0; i<Math.min(ids.length, 3); i++) {
					url += 'phone_'+i+'='+ids[i]+'&';
				}
				url += 'from='+encodeURIComponent(document.location);
				url = url.replace(/&$/, '');
				window.location = url;
			}
		}
	});

	$('#ozji_izbor .phone').live('mouseover', function(e) {
		$(this).addClass('over');
	});

	$('#ozji_izbor .phone').live('mouseout', function(e) {
		$(this).removeClass('over');
	});
	
	$('#ozji_izbor .phone').live('click', function(e) {
		e.preventDefault();

		var id = $(this).attr('data-id');

		var ids = $.cookie('phone_compare');
		if (ids) {
			ids = ids.replace(/\s+/, '').split(/,/);
		} else {
			ids = [];
		}
		var pos = ids.indexOf(id);
		if (pos!=-1) {
			ids.splice(pos, 1);
			$.cookie('phone_compare', ids.join(','), comparePhoneCookieOpts);
		}

		$('.add-to-phone-compare[data-id="'+id+'"]').removeClass('selected');
		$(this).remove();

		return false;
	});

	$('select.styled_select').customStyle();

	if ($('#primerjalnik').size()>0) {
		var cached_loads = {};

		$('#primerjalnik form select').change(function() {
			var id;
			var form = $('#primerjalnik form');
			var hash = [];

			// setup hash text
			for (var i=0; i<3; i++) {
				id = parseInt($('select[name=phone_'+i+']', form).val());
				if (id>0) {
					hash.push(id);
				} else {
					hash.push('');
				}
			}
			
			window.location.hash = '#'+hash.join(',');
			if ($.browser.msie) {
				$(window).trigger('hashchange');
			}
		});

		
		$(window).bind('hashchange', function() {
			
			var ids = window.location.hash.replace(/^#/, '').split(',');
			var i, span;

			// got ids from hash
			if (ids.length==3) {

			// parse them from $_GET
			} else {
				var m;
				var ids = [];
				for (i=0; i<3; i++) {
					m = window.location.search.match(new RegExp('phone_'+i+'=(\\d+)'));
					if (m) {
						ids.push(m[1]);
					} else {
						ids.push(-1);
					}
				}
			}

			// set select to correct values
			var anyChanged = false;
			for (i=0; i<3; i++) {
				var old = $('#primerjalnik form select[name=phone_'+i+']').attr('data-id');
				if (old!=ids[i]) {
					var select = $('#primerjalnik form select[name=phone_'+i+']');
					if (ids[i]) {
						select.val(ids[i]);
					} else {
						select.val(-1);
					}

					// redraw fanncy select
					span = $('#primerjalnik form select[name=phone_'+i+']').next('span');
					$('span', span).text($('#primerjalnik form select[name=phone_'+i+']').find(':selected').text()).parent().addClass('changed');

					// load correct info in box
					var container = $('#primerjalnik .top .phone.pos_'+i);
					if (ids[i]>0) {
						container.attr('href', ALL_PHONES[ids[i]].url);
						$('.title', container).html(ALL_PHONES[ids[i]].title);
						$('.price', container).html(ALL_PHONES[ids[i]].price);
						$('img', container).attr('src', ALL_PHONES[ids[i]].image);
						$('img', container).attr('title', ALL_PHONES[ids[i]].title);
						container.show();
					} else {
						container.hide();
					}
					container.attr('data-id', ids[i]);

					// only reload if loaded items are one selected
					anyChanged = true;
				}
			}

			if (anyChanged) {
				var id;
				var url = 'index.php?ajax=comparator';
				var form = $('#primerjalnik form');

				for (var i=0; i<3; i++) {
					id = $('select[name=phone_'+i+']', form).val();
					if (id) {
						url += '&phone_'+i+'='+id;
					}
				}

				if (typeof(cached_loads[url])!='undefined') {
					$('#primerjalnik .podatki').html(cached_loads[url]);
				} else {
					$.ajax({
						url:url,
						dataType: 'html',
						success: function(rsp) {
							$('#primerjalnik .podatki').html(rsp);
							cached_loads[url] = rsp;
						}
					});
				}

				
			}
		});

		// ensure correct phones are display if user came with hashed link
		$(window).trigger('hashchange');
	}
	
	$('.detail-phone-right .properties .tabs a').click(function(e) {
		e.preventDefault();
		$(this).blur();

		var id = $(this).attr('data-id');

		$('.detail-phone-right .properties .tabs li').removeClass('selected');
		$(this).closest('li').addClass('selected');

		$('.detail-phone-right .properties .items > li').hide();
		$('.detail-phone-right .properties .items > li[data-id="'+id+'"]').show();
	});

	$('.detail-phone-right .properties .items li li a.title').click(function(e) {
		e.preventDefault();
		$(this).blur();

		var thisLi = $(this).closest('li');
		var thisUl = $(thisLi).closest('ul');

		if ($('.text', thisLi).is(':visible')) {
			return;
		}

		$('li .text', thisUl).slideUp();
		$('.text', thisLi).slideDown();

		$('li', thisUl).removeClass('selected');
		$(thisLi).addClass('selected');
	});

	$('.detail-phone-right .properties .items .video').each(function() {
		var videoFile = $(this).attr('data-file');
		var id = 'video_'+_writeVideoPlayerInc;
		$(this).attr('id', id);
		writeVideoPlayer(id, videoFile);
	});

	// real browsers support css 3 selectors
	if (jQuery.browser.msie) {
		$('.detail-phone-right .properties .items li table').each(function() {

			$('tr:odd', this).addClass('even');
			$('tr', this).each(function() {
				$('td:first', this).addClass('first');
			});
		});

		$('.paket-cenik table tr:last').addClass('last');
		$('.paket-cenik table tr').each(function() {
			$('td:first, th:first', this).addClass('first');
		});
	}

	if($.browser.msie && $.browser.version=="6.0") {
		var dropdown_timers = {};

		$('#paket-detajl .dropdown').hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		});
	}

	$('a.app-link').click(function() {
		if (this.id.length > 16) {
			openApp(this.id);
		}
		return false;
	});
	
	function openApp(conf) {
		var ver = $('#kalkulator').attr('rel');
		if (ver == 5) {
			swfobject.embedSWF("flash/kalkulator5.swf?v="+(window.debitel_app_version)+"&choice="+conf, "kalkulator", "960", "520", "9.0.0", "expressInstall.swf"/*, {}, {wmode: "transparent"}*/);
		} else {
			swfobject.embedSWF("flash/kalkulator.swf?v="+(window.debitel_app_version)+"&choice="+conf, "kalkulator", "960", "520", "9.0.0", "expressInstall.swf"/*, {}, {wmode: "transparent"}*/);
		}
		$('#kalkulator').colorbox({inline:true, href:'#kalkulator', width:'960', height:'550', opacity:0.7, transition:"none", open:true, scrolling: false});
		$('#kalkulator').show();
	}
	
	/* hover za tabelo cenikov */
	$('.cenik table td.price-hover').hover(
		function() {
			$('a', this).css({color: '#fff100'}).append('<span>Nadaljuj</span>');
			$(this).siblings('th').css({color: '#fff100'});
			var i =  $(this).prevAll().length + 1;
			$('.cenik tr.top th:nth-child('+i+')').css({color: '#fff100'});
		},
		function () {
			$('a', this).css({color: '#ffffff'});
			$(this).siblings('th').css({color: '#fff'});
			var i =  $(this).prevAll().length + 1;
			$('.cenik tr.top th:nth-child('+i+')').css({color: '#fff'});
			$('a span', this).remove();
		}
	);
	
	frag = (document.location.toString()).split("#");
	if (frag.length == 2) {
		conf = (frag[1].toString()).split("=");
		if (conf.length == 2 && conf[0] == "konfiguracija") {
			openApp(conf[1]);
		}
	}
	
	$('.roletke .item .title').click(function(e) {
		e.preventDefault();
		$(this).blur();
		
		var thisLi = $(this).closest('.item');
		var thisUl = $(thisLi).closest('.roletke');

		if ($('.text', thisLi).is(':visible')) {
			return;
		}

		$('.item .text', thisUl).slideUp();
		$('.text', thisLi).slideDown();

		$('.item', thisUl).removeClass('selected');
		$(thisLi).addClass('selected');
	});
	
/*	$('.cenik .left[title], .cenik td[title]').tooltip({
		position: 'center right',
		offset: [0,0],
		delay:0
	});*/

	
});

var _writeVideoPlayerInc = 0;
function writeVideoPlayer(id, videoFile, previewFile, w, h) {
	if (typeof(w)=='undefined' || typeof(h)=='undefined') {
		w = '480';
		h = '320';
	}

	var jsId = 'video_'+_writeVideoPlayerInc;
	_writeVideoPlayerInc++;

	var so = new SWFObject('player.swf', jsId,w,h,'8');
    so.addParam('allowscriptaccess','always');
    so.addParam('allowfullscreen','true');
    so.addParam('wmode','transparent');
    so.addVariable('width','480');
    so.addVariable('height','320');
    so.addVariable('file',videoFile);
    so.addVariable('image',previewFile);
    so.addVariable('javascriptid',jsId);
    so.addVariable('enablejs','true');
    //so.addVariable('backcolor', 'ffffff');
    //so.addVariable('screencolor', 'ffffff');
    //so.addVariable('skin', 'comet.swf');
    so.addVariable('controlbar', 'over');
    so.addVariable('mute', 'false');
    so.addVariable('autostart', 'false');
    so.write(id);

}

