jQuery.fn.exists = function(){return this.length > 0;};

$.fn.extend({
    fire: function(evttype){
        el = this.get(0);
        if (document.createEvent) {
            var evt = document.createEvent('HTMLEvents');
            evt.initEvent(evttype, false, false);
            el.dispatchEvent(evt);
        } else if (document.createEventObject) {
            el.fireEvent('on' + evttype);
        }
        return this;
    }
});

//phone filters
$(document).ready(function(){
	if($('#phone-filters').exists()){
		phoneFilters();
		submitFilter();
		phoneFilterStats();
	}
	if($('.the-phone').exists()){
		phoneStats();
	}
	if($('.package-wrap').exists()){
		packageStats();
		squeezePackageTable();
	}
	$('.tab-content').tablePricing();
	$('.package-wrap').tablePricing();
	$('.calculator-package .calls-config td.basic').activateRelativesToo('.compare-content');
	$('.calculator-package .data-config td.basic').activateRelativesToo('.compare-content');
	var tabVal = $.query.get('tab');
	if(tabVal){
		$('.tabs li:eq(' + (parseInt($.query.get('tab'), 10 ) - 1) +')').click();
	}
	if($.query.get('msg')){
		highlightFor($('.p' + tabVal + ' .sms-config tr:eq(0)').find('td:eq(' + $.query.get('msg') + ')'));
		$('.p' + tabVal + ' .sms-config tr:eq(0)').find('td:eq(' + $.query.get('msg') + ')').click();
	}
	if($('.package-wrap').exists()){
//        PSMB-1441 - preselect M ZAME for duration of action
		highlightFor($('.calls-config tr:eq(0)').find('td:eq(1)'));
		$('.calls-config tr:eq(0)').find('td:eq(1)').click();
		if ($.query.get('col') !== '') {
			highlightFor($('.calls-config tr:eq(0)').find('td:eq(' + $.query.get('col') + ')'));
			$('.calls-config tr:eq(0)').find('td:eq(' + $.query.get('col') + ')').click();
		}
		if ($.query.get('data') !== '') {
			highlightFor($('.data-config tr:eq(0)').find('td:eq(' + $.query.get('data') + ')'));
			$('.data-config tr:eq(0)').find('td:eq(' + $.query.get('data') + ')').click();
		}
		if ($.query.get('msg') !== '') {
			highlightFor($('.sms-config tr:eq(0)').find('td:eq(' + $.query.get('msg') + ')'));
			$('.sms-config tr:eq(0)').find('td:eq(' + $.query.get('msg') + ')').click();
		}
	}
	if($.query.get('row') || $.query.get('col')){
		var row = parseInt($.query.get('row'), 10);
		var col = parseInt($.query.get('col'), 10);
		highlightFor($('.p' + tabVal + ' .package-config tr:eq(' + row +')').find('td:eq(' + col + ')'));
		$('.p' + tabVal + ' .package-config tr:eq(' + row +')').find('td:eq(' + col + ')').click();
	} else {
		if($('.tab-content .pricing .r2 td:first').exists()){
			if($('.tab-content .pricing .r2 td.basic:first').hasClass('hidden')){
				highlightFor($('.tab-content:visible .pricing .r2 td.basic').eq(0));
				$('.tab-content:visible .pricing .r2 td.basic').eq(0).click();
			} else {
				highlightFor($('.tab-content:visible .pricing .r2 td.basic:first'));
				$('.tab-content:visible .pricing .r2 td.basic:first').click();
			}
		}
		if($('.package-wrap .pricing .r2 td:first').exists()){
			if($('.package-wrap .pricing .r2 td.basic:first').hasClass('hidden')){
				highlightFor($('.package-wrap .pricing .r2 td.basic').eq(1));
				$('.package-wrap .pricing .r2 td.basic').eq(1).click();
			} else {
				highlightFor($('.package-wrap .pricing .r2 td.basic:first'));
				$('.package-wrap .pricing .r2 td.basic:first').click();
			}
		}
		if($('.tab-content:visible .pricing td.best').exists()){
			highlightFor($('.tab-content .pricing td.best'));
			$('.tab-content .pricing td.best').click();
		}
	}
	$('.calculator-phone ul.tabs').tabs('.tab-content');
	$('.calculator-package ul.tabs').tabs('.compare-content');

	if($('.gallery .images').exists())initPhoneGalleryScroll();
	if(jQuery.fancybox){
		$(".images li a,.gallery .zoom").fancybox({
			'titleShow' : false,
			'padding' : 30,
			'onComplete' : function(){
				if($('#fancybox-left').exists()){
					$('#fancybox-left:not(.bound)').click(function(){
						pageTracker._trackEvent('telefon', 'telefon - naprej-nazaj', 'lightbox');
					}).addClass('bound');
				}
				if($('#fancybox-right').exists()){
					$('#fancybox-right:not(.bound)').click(function(){
						pageTracker._trackEvent('telefon', 'telefon - naprej-nazaj', 'lightbox');
					}).addClass('bound');
				}
			}
			});
		$(".expand-properties").fancybox({
			'scrolling' : 'no',
			'autoScale' : false,
			'onComplete' : function(){
				if($('#fancybox-inner #phone-full-list').exists()){
					$('#fancybox-inner #phone-full-list .phone-full-wrap .r .print').click(function(){
						pageTracker._trackEvent('telefon', 'telefon - print', '');
					});
				}
			}
		});
	}
	if($('.the-phone .to-favorite').exists()){
		$('.the-phone .to-favorite').click(function(e){
				e.preventDefault();
				addToBasket($(this));
			});
	}
	if($('.my-phones .content ul li .remove').exists()){		
		$('.my-phones .content ul li .remove').click(function(e){
				e.preventDefault();
				addToBasket($(this));
			});
		}
	if($('.my-phones .content .remove-all').exists()){
		$('.my-phones .content .remove-all').click(function(e){
			e.preventDefault();
			addToBasket($(this));
		});
	}
	if($('.fw-button').exists()){
		$('.fw-button').click(function(e){
			e.preventDefault();
			var href = $(this).attr('href');
			href = href.replace('packID', $('#pass_params_form .pack_id_input').val());
			href = href.replace('upgradeID', $('#pass_params_form .upgrade_id_input').val());			
			window.location = href;
		});
	}
	if($('#perm_link').exists()){
		$('#perm_link').click(function(e){
			e.preventDefault();
			shareOnFacebook("http://www.simobil.si" + $(this).attr("href") , "Simobil", "");
		});
	}
    if($('.ispecial').exists()){
        $('.ispecial em label').each(function(){
          $(this).text($(this).text().replace("MB", "GB"));
        });
    }
    if($(".amis").length > 0){
        $('.print-content').detach();
        $('#content h2').each(function(){
           if($(this).text().trim() == 'Novi paketi ZAME'){
               $(this).text('Paketi ZAME');
           }
           if($(this).text().trim() == 'Novi paketi PODJETNI'){
               $(this).text('Paketi PODJETNI');
           }
           if($(this).text().trim() == 'O paketih ORTO'){
               $(this).text('Paketi ORTO');
           }
        });
    }
});

var phoneFilters = function(){
	var w = $('#phone-list');
	$('.basic .extend-filters, .submit-filters .more',w).click(function(e){
		e.preventDefault();
		$('#phone-filters',w).removeClass('basic').addClass('extended');
		var animator = $('.animator',w);
		if(animator.hasClass("business")){
			$('.animator',w).animate({height: '320px'}, 1000, function(){});
		} else {
			$('.animator',w).animate({height: '380px'}, 1000, function(){});
		}
	});
	$('.submit-filters .less',w).click(function(e){
		e.preventDefault();
		$('.animator',w).animate({height: '135px'}, 600, function(){
			$('#phone-filters',w).removeClass('extended').addClass('basic');
		});
	});
	$('#pkgs3',w).change(function(e){
		if($(this).val() == '8838A994-DDA6-ABF3-2081-E93A930BE2F8'){
			$('#pkgs3-1',w).val("none");
			$('#berry_hide',w).hide();
		} else {
			$('#berry_hide',w).show();
		}
	});	
	$('.package-filter li input[type="radio"]',w).click(function(){
		var l = $(this);
		if(!l.hasClass('on') && !l.hasClass('off')){
			l.addClass('on');
		}
		if(l.hasClass('on')){
			l.removeClass('on');
			l.addClass('off');
			l.parents('ul').find('li').removeClass('active');
			l.parents('li').addClass('active');
			$(this).attr('checked', true);
		} else if(l.hasClass('off')){
			l.removeClass('off');
			l.addClass('on');
			l.parents('ul').find('li').removeClass('active');
			$(this).attr('checked', false);
		}
	});
	var submiters = $('.part1 fieldset input, fieldset.package-filter input',w);
	submiters.click(submitFilter);
	$('fieldset.package-filter select').change(submitFilter);

    $('#phone-filters .make :checkbox').change(function(){
        if($('#phone-filters .make #f1').is(':checked')){
            if($('#phone-filters .make :checked').length == 1){
                $('#phone-filters .package-filter .upgrds option').each(function(){
                    if($(this).val() == 'blackberry' || $(this).val() == 'none'){
                        $(this).show();
                    } else {
                        var placeholder = $(this).parent().parent().find('.placeholder');
                        var el = $(this).detach();
                        el.appendTo(placeholder);
                    }
                });
            } else {
              $('#phone-filters .package-filter .placeholder').each(function(){
                  $(this).find('option').each(function(){
                      var upgrds = $(this).parent().parent().find('.upgrds');
                      $(this).detach().appendTo(upgrds);
                  });
              });
            }
        } else {
            $('#phone-filters .package-filter .placeholder').each(function(){
                  $(this).find('option').each(function(){
                      var upgrds = $(this).parent().parent().find('.upgrds');
                      $(this).detach().appendTo(upgrds);
                  });
              });
        }
    });

	var sorters = $('.submit-filters .price-sort, .submit-filters .make-sort',w);
	sorters.click(function(e){
		e.preventDefault();
		var l = $(this);
		if(!l.hasClass('asc') && !l.hasClass('desc')){
			l.addClass('asc');
		}
		var order = l.hasClass('desc') ? 'asc' : 'desc';
//		l.toggleClass('asc desc');
		if(l.hasClass('asc')){
			l.removeClass('asc');
			l.addClass('desc');
		} else if(l.hasClass('desc')){
			l.removeClass('desc');
			l.addClass('asc');
		}
		l.siblings('a').removeClass('asc').removeClass('desc');
		$('input.sort-order').val(order);
		$('input.sort-by').val(l.attr('href').split('#')[1]);
		submitFilter();
	});
};

var submitFilter = function(){
	var w = $('#phone-list');
	var url = $('form#phone-filters').attr('action');
	var href = window.location.href;
	if(href.indexOf('pass=pr') > -1){
		$('.submit-filters .more',w).click();
		$('.pass_params').remove();
	}
	var filterData = $('form#phone-filters').serializeArray();
	$('#phones',w).addClass('loading');
	$.ajax({
	  type: 'POST',
	  url: url,
	  data: filterData,
	  success: function(data){
		$('#phones ul',w).remove();
		$('#phones .loading',w).after(generatePhones(data.phones));
		$('.pages',w).html(generatePaging(data.pages, data.current_page));
	    $('.pages li').click(function(e){
			e.preventDefault();
            var scrollTarget = $(this).find("a").attr("href");
            setTimeout(function() {
                $("html:not(:animated), body:not(:animated)").animate({ scrollTop: $(scrollTarget).offset().top}, 0);
		    }, 100);
			var l = $(this);			
			$('input.page-num').val(l.find('a').text());
			pageTracker._trackEvent('telefoni', 'stran', l.find('a').text());
			submitFilter();
		  });
		w.find('#phones ul li .wr h3 a').click(function(){
			pageTracker._trackEvent('telefoni', 'telefon - Več', $(this).text());
		});
	  	w.find('#phones ul li .wr .r .add-to-favorites').click(function(){
			pageTracker._trackEvent('telefoni', 'telefon - Dodaj v izbor', $(this).parent().parent().find('h3 a').text());
		});
	  	if(!(data.phones.length > 0)){
		  $('#phones p',w).show();
	    } else{
		  $('#phones p',w).hide();
		  }
		if($('.calculator-grid #phones li .add-to-favorites').exists()){
			$('.calculator-grid #phones li .add-to-favorites').click(function(e){				
				e.preventDefault();
				addToBasket($(this));
			});
		}
		$('#phones',w).removeClass('loading');
	  },
	  dataType: 'json'
	});

};

var addToBasket = function(el){
	var url = el.attr('href');
	el.addClass('hidden');
	$('.my-phones content').addClass('loading');
	$.ajax({
	  type: 'POST',
	  url: url,
	  data: 'guid=1',
	  success: function(data){		
		$('#basket_wrapper').html(data);
		$('.my-phones content').removeClass('loading');
   	    if($('.my-phones .content ul li .remove').exists()){
			$('.my-phones .content ul li .remove').click(function(e){
				e.preventDefault();
				var id = $(this).attr('id');
				id = id.replace('ph-', '');
				id = 'add-ph-'+id;
				$('#'+id).removeClass('hidden');
				addToBasket($(this));
			});
		}
	    if($('.my-phones .content .remove-all').exists()){
			$('.my-phones .content .remove-all').click(function(e){
				e.preventDefault();
				$('.calculator-grid #phones li .add-to-favorites').removeClass('hidden');
				addToBasket($(this));
			});
		}
	  },
	  dataType: 'html'
	});	
	if(window.location.href.indexOf("linkid=compare") > -1){
		setTimeout('window.location.reload()', 500);
	}
};

//phone grid
var generatePhones = function(d){
	var html = '<ul>';
	$(d).each(function(i,e){
		i+=1;var liclass = '';
		if (i % 4 == 0)liclass += 'last';
		if(e.badge == 'new')liclass += ' greyed';
		if(liclass != '')liclass = ' class="'+liclass+'"';
		price = e.price + ''.replace('.',',');
		html += '<li'+liclass+'>';
		html += '<div class="wr">';
		if(e.badge == 'soon')html += '<div class="tag new"></div>';
		if(e.badge == 'new')html += '<div class="tag comming"></div>';
		html += '<h3><a href="'+e.phone_link+'">'+e.title+'</a></h3>';
		html += '<div class="image">';
		html += '<a href="'+e.phone_link+'"><img src="'+e.image_link+'" alt="'+e.title+'"/></a>';
		html += '</div>';
		html += '<div class="r">';
		html += '<span class="price">'+price+' &euro;</span><br />';
		html += '<em>na paketu</em><br />';
		html += '<strong>'+e.package+'</strong>';
		html += '<a href="'+e.add_to_selection_link+'" class="add-to-favorites" id="add-ph-' + e.ph_id + '">Dodaj v izbor</a>';
		html += '</div>';
		html += '</div>';
		html += '</li>';
	});
	html = html + '</ul>';
	return html;
};

//phone grid paging
var generatePaging = function(n, sel){
	if(!sel)sel = 1;
	var html = '<ul>';
	for(i = 1;i <= n;i++){
		var act = '';
		if(i == sel)act = ' active';
		html += '<li class="p'+i+''+act+'"><a class="paginator" href="#phone-list">'+i+'</a></li>';
	}
	html += '</ul>';
	return html;
};

$.fn.tabs = function(content) {

	var highlightFor = function(c){
		var ind = c.prevAll().length + 1;
		c.parent().parent().find('td').removeClass('active');
		c.addClass('active');
		c.parent().find('td:first').addClass('active');
		c.parent().parent().find('tr:first').find('td:nth-child('+ind+')').addClass('active');
	};

	return this.each(function() {
		var c = $(content);if(!c.exists())return;
		var t = $(this);
		t.find('li a').click(function(e){e.preventDefault()});
		t.find('li').click(function(){
			var link = '/mobilePhones.cp2';
			var newQuery = $.query.SET('tab', parseInt($(this).parent().find('li').index($(this)), 10) + 1);
			$('#perm_link').attr('href', link + newQuery);
			if($(this).hasClass('active'))return;
			$(this).find('a').click(function(e){e.preventDefault()});
			c.hide();
			$(content+'.'+$(this).attr('class')).removeClass('hidden').show();
			if($(content+'.'+$(this).attr('class') + ' .pricing td.best').exists()){
				highlightFor($(content+'.'+$(this).attr('class') + ' .pricing td.best'));
				$(content+'.'+$(this).attr('class') + ' .pricing td.best').click();
			} else if($(content+'.'+$(this).attr('class') + ' .pricing .r2 td:first').exists()){
				if($(content+'.'+$(this).attr('class') + ' .pricing .r2 td.basic:first').hasClass('hidden')){
					highlightFor($(content+'.'+$(this).attr('class') + ' .pricing td.basic').eq(1));
					$(content+'.'+$(this).attr('class') + ' .pricing td.basic').eq(1).click();
				} else {
					highlightFor($(content+'.'+$(this).attr('class') + ' .pricing td.basic:first'));
					$(content+'.'+$(this).attr('class') + ' .pricing td.basic:first').click();
				}
			}
			t.find('li').removeClass('active');
			$(this).addClass('active');
		});
	});
};

var highlightFor =  function (c){
	var ind = c.prevAll().length + 1
	c.parent().parent().find('td').removeClass('active');
	c.addClass('active');
	c.parent().find('td:first').addClass('active');
	c.parent().parent().find('tr:first').find('td:nth-child('+ind+')').addClass('active');
};

//plan table highlighitng and pricing
$.fn.tablePricing = function() {
	
	var deselectAll = function(table){
		table.find('td.active').removeClass('active');
	};
	
	return this.each(function() {
		var wrap = $(this);
		var tb = wrap.find('.pricing');
		var b = tb.find('td.basic');
		var price_holder = wrap.find('.prices-sum');
		b.hover(function(){highlightFor($(this));},function(){});
		tb.hover(function(){},function(){
			var this_tb = $(this);
			if(this_tb.find('td.selected').exists())reselect = this_tb.find('td.selected');
			else reselect = this_tb.find('td.best');
			if(!reselect.exists())deselectAll(this_tb);
			else highlightFor(reselect)
		});
		b.click(function(){
			var p = $(this);
			var this_tb = p.parents('table');
            var link = '/mobilePhones.cp2';
			if(this_tb.hasClass('sms-config')){
				var newQuery = $.query.SET('msg', $(this).parent().find('td').index($(this)));
			} else if(this_tb.hasClass('data-config')){
				var newQuery = $.query.SET('data', $(this).parent().find('td').index($(this)));
			} else {
				var newQuery = $.query.SET('col', $(this).parent().find('td').index($(this)));
				newQuery = $.query.SET('row', $(this).parent().parent().find('tr').index($(this).parent()));
			}
			this_tb.find('td').removeClass('selected');
			$(this).addClass('selected');
             if (this_tb.parent().parent().hasClass('package-wrap')){
                link = '/inside.cp2';
                var m_title = "XS ZAME";
                if(this_tb.parent().find('.calls-config .selected strong').html() != null){
                    m_title = this_tb.parent().find('.calls-config .selected strong').html();
                }
                if(this_tb.parent().find('.data-config .selected strong').html() != null){
                    m_title = m_title + " " + this_tb.parent().find('.data-config .selected strong').html();
                }
                newQuery = $.query.SET('meta_fb_title', m_title);
                newQuery = $.query.SET('meta_fb_desc', 'Moj paket pri Si.mobilu.');
            }
			$('#perm_link').attr('href', link + newQuery);
			//selected phone price
			new_phone_price = p.find('.phone-price');
			if(new_phone_price.exists())
				price_holder.find('.phone-price-sum').text(new_phone_price.text());
			//selected plan prices
			var plan_price = 0;
			wrap.find('td.active .price-add').each(function(){
				var a = parseFloat(($(this).text()).replace(",", "."));
				plan_price += a;
			});
            plan_price = plan_price + "";
			price_holder.find('.month-price-sum').text(plan_price.replace(".", ","));
			price_holder.animate({opacity: 0.25 }, 300);
			price_holder.animate({opacity: 1 }, 0 );

			//selected package names
            if(wrap.find('td.active .add-pckg1').parent().hasClass('ortosmart')){
                $('.pricing .r2 .opt201').removeClass("selected active");
                $('.pricing .r2 .opt201').hide();
            } else {
                $('.pricing .r2 .opt201').show();
            }
			if(wrap.find('td.active .add-pckg2').exists()){
				wrap.find('.sel-pckg2').text(wrap.find('td.active .add-pckg2').text());
			}
			if(wrap.find('td.active .add-pckg1').exists()){
				wrap.find('.sel-pckg1').text(wrap.find('td.active .add-pckg1').text());
			}
            if(wrap.find('td.active .add-pckg1 a').exists()){
				wrap.find('.pack-more-link').attr("href", wrap.find('td.active .add-pckg1 a').attr("href"));
			}
			//whad do i get? data, calls and sms (try to optimize)
			if(wrap.find('td.active .add-data').exists()){
				var output = '';
				if (wrap.find('td.active .add-data').length > 1){
                    if(wrap.find('td.active .add-xmas-other').exists()){
                        var outParsed = 0;
                        wrap.find('td.active .add-data').each(function(index){
                            if(index != 1 && wrap.find('td.active .add-data').parent().parent().hasClass('ispecial')){
                                if($(this).text() == "3,5"){
                                    outParsed += 3584;
                                } else if($(this).text() == "5,5") {
                                    outParsed += 5632;
                                } else if($(this).text() == "4,5") {
                                    outParsed += 4608;
                                } else {
                                    outParsed += parseInt($(this).text(), 10) * 1024;
                                }
                            } else {
                                outParsed += parseInt($(this).text(), 10);
                            }
					    });
                        output = outParsed + "";
                    } else {
					wrap.find('td.active .add-data').each(function(index){
							output = output + wrap.find('td.active .add-data').eq(index).text();
							if(index != 1){
                                if(wrap.find('td.active .add-data').parent().parent().hasClass('ispecial')){
                                    output = output + " GB";
                                }
								output = output + ' + ';
							}
					    });
                    }
				} else {
					output =  wrap.find('td.active .add-data').text();
                    if(wrap.find('td.active .add-data').parent().parent().hasClass('ispecial')){
                        wrap.find('.sel-data-wrap label').text(wrap.find('.sel-data-wrap label').text().replace("MB", "GB"));
                    } else {
                        wrap.find('.sel-data-wrap label').text(wrap.find('.sel-data-wrap label').text().replace("GB", "MB"));
                    }
				}
                if(wrap.find('td.active .add-xmas-other').exists()) {
                    wrap.find('.sel-data-wrap label').text(wrap.find('.sel-data-wrap label').text().replace("GB", "MB"));
                }
				wrap.find('.sel-data').text(output);
				wrap.find('.sel-data-wrap.hidden').removeClass('hidden').hide().fadeIn();
				wrap.find('.sel-data-wrap').fadeIn();
			} else {
				wrap.find('.sel-data-wrap').fadeOut();
			}
			if(wrap.find('td.active .add-time').exists()){
				if(wrap.find('td.active .add-pckg1').text() == 'XS ZAME'){
					wrap.find('.sel-time-wrap').html(wrap.find('.sel-time-wrap').html().replace('ostala ', ''));	
				} else {
					wrap.find('.sel-time-wrap').html(wrap.find('.sel-time-wrap').html().replace('vsa omrežja', 'vsa ostala omrežja'));
				}
                if(wrap.find('td.active .add-pckg1').text() == 'ORTO SMART' && wrap.find('td.active .add-time').length > 1){
                    var outTime = '';
                    wrap.find('td.active .add-time').each(function(index){
                        outTime = outTime + wrap.find('td.active .add-time').eq(index).text();
                        if(index != 1){
                            outTime = outTime + ' + ';
                        }
                    });
                    wrap.find('.sel-time').text(outTime);
                } else {
				wrap.find('.sel-time').text(wrap.find('td.active .add-time').text());
                }
				wrap.find('.sel-time-wrap.hidden').removeClass('hidden').hide().fadeIn();
				wrap.find('.sel-time-wrap').fadeIn();
			} else {
				wrap.find('.sel-time-wrap').fadeOut();
			}
            if(wrap.find('td.active .add-xmas-other').exists() || wrap.find('td.active .add-pckg1').text() == 'XS ZAME'){
                wrap.find('.sel-time-wrap .sel-other-network').hide();
            } else {
                wrap.find('.sel-time-wrap .sel-other-network').show();
            }
			if(wrap.find('td.active .add-other').exists()){
				var prev_text = wrap.find('.sel-other').text();
				var new_text = wrap.find('td.active .add-other').html();
				if(new_text.indexOf('BIS') > -1){
					new_text = 'storitev BlackBerry BIS';
				}
				if(new_text.indexOf('Vodafone') > -1){
					new_text = 'več kot 3.000.000 melodij, <br /> brezplačni dostop do Vodafone live!';
					wrap.find('.sel-other').html(new_text);
				}else {
					wrap.find('.sel-other').html(new_text);
				}
				wrap.find('.sel-other-wrap.hidden').removeClass('hidden').hide().fadeIn();
				wrap.find('.sel-other-wrap').fadeIn();
			} else {
				wrap.find('.sel-other-wrap').fadeOut();
			}			
			if(wrap.find('td.active .add-sms-special').exists()){
				wrap.find('.sel-sms-special').text(wrap.find('td.active .add-sms-special').text());
				wrap.find('.sel-sms-special-wrap.hidden').removeClass('hidden').hide().fadeIn();
				wrap.find('.sel-sms-special-wrap').fadeIn();
			} else {
				wrap.find('.sel-sms-special-wrap').fadeIn();
			}
			if(wrap.find('td.active .add-sms').exists()){
                if(!(wrap.find('td.active .add-pckg1').text().indexOf('ORTO') > - 1)){
				var output = '';
				if (wrap.find('td.active .add-sms').length > 1){
                    if (wrap.find('td.active .add-sms-parse').exists()){
                        var outParsed = 0;
                        wrap.find('td.active .add-sms').each(function(index){
                            if(!$(this).hasClass('add-sms-parse')){
                                outParsed += parseInt($(this).text(), 10);
                            }
                        });
                        wrap.find('td.active .add-sms-parse').each(function(index){
                            outParsed += parseInt($(this).text(), 10);
                        });
                        output = outParsed + "";
                    } else {
					wrap.find('td.active .add-sms').each(function(index){
							output = output + wrap.find('td.active .add-sms').eq(index).text();
							if(index != 1){
								output = output + ' + ';
							}
					})
                    }
				} else {
                    if (wrap.find('td.active .add-sms-parse').exists()){
                        var outParsed = 0;
                        wrap.find('td.active .add-sms').each(function(index){
                            if(!$(this).hasClass('add-sms-parse')){
                                outParsed += parseInt($(this).text(), 10);
                            }
                        });
                        wrap.find('td.active .add-sms-parse').each(function(index){
                            outParsed += parseInt($(this).text(), 10);
                        });
                        output = outParsed + "";
				} else {
					output =  wrap.find('td.active .add-sms').text();
				}
				}
				wrap.find('.sel-sms').text(output);
				wrap.find('.sel-sms-wrap.hidden').removeClass('hidden').hide().fadeIn();
				wrap.find('.sel-sms-wrap').fadeIn();
                }
			} else {
				wrap.find('.sel-sms-wrap').fadeOut();
			}
			if(wrap.find('td.active .add-except').exists()){
				if(wrap.find('td.active .add-except .price-except').exists()){
					wrap.find('.sel-except-price').html(wrap.find('td.active .add-except .price-except').html());
					wrap.find('.sel-except-price span').removeClass('hidden');
					wrap.find('.calls .sel-except-wrap.hidden').removeClass('hidden').hide().fadeIn();
					wrap.find('.calls .sel-except-wrap').fadeIn();
				} else {
					wrap.find('.calls .sel-except-wrap').fadeOut();
				}
				if(wrap.find('td.active .add-except .sms-except').exists()){
					wrap.find('.sel-except-sms').html(wrap.find('td.active .add-except .sms-except').html());
					wrap.find('.sel-except-sms span').removeClass('hidden');					
					wrap.find('.sms .sel-except-wrap.hidden').removeClass('hidden').hide().fadeIn();
					wrap.find('.sms .sel-except-wrap').fadeIn();
				} else {
					wrap.find('.sms .sel-except-wrap').fadeOut();
				}
			}
			if(wrap.find('td.active .unlimited-si').exists()){
                if(wrap.find('td.active .add-pckg1').text().indexOf('ORTO') > - 1){
                    if(wrap.find('td.active .add-pckg1').parent().hasClass('ortosmart')){
                        wrap.find('.unlimited-wrap').html(wrap.find('.unlimited-wrap').html().replace('1001 min', 'neskončno'));
                        wrap.find('.sel-sms-special-wrap strong').html("neskončno SMS in MMS");
                    } else {
                        wrap.find('.unlimited-wrap').html(wrap.find('.unlimited-wrap').html().replace('neskončno', '1001 min'));
                    wrap.find('.unlimited-wrap').html(wrap.find('.unlimited-wrap').html().replace('0 €/min', '1001 min'));
                        wrap.find('.sel-sms-special-wrap strong').html("1001 SMS in 1001 MMS sporočilo");
                    }
//                    wrap.find('.c0 .unlimited-si').html('<span>1001</span> min v Si.mobil');

                }
				wrap.find('.unlimited-wrap.hidden').removeClass('hidden').hide().fadeIn();
				wrap.find('.unlimited-wrap').fadeIn();
			} else {
				wrap.find('.unlimited-wrap').fadeOut();
			}
			if(wrap.find('td.active .pack_id').exists()){
				wrap.find('#pass_params_form .pack_id_input').val(wrap.find('td.active .pack_id').text());
			}
			if(wrap.find('td.active .upgrade_id').exists()){
				wrap.find('#pass_params_form .upgrade_id_input').val(wrap.find('td.active .upgrade_id').text());				
			}
		});
	});
};

//activate tags with same classes
$.fn.activateRelativesToo = function(target_wrap){
	return this.each(function(){
		//exclude active,basic class from processing
		var e = $(this);
		e.click(function(){
			var t = $(target_wrap);
			var call_class = '';
			var data_class = '';
			var classes = $.grep(e.attr('class').split(' '), function(v,i){return (v != 'active' && v != 'basic' && v != 'selected')});
			if(e.parent().parent().parent().hasClass('calls-config')){
				call_class = classes;
				if($('.data-config td.active:not(.c0)').length > 0){
					data_class = $.grep($('.data-config td.active:not(.c0)').attr('class').split(' '), function(v,i){return (v != 'active' && v != 'basic' && v != 'selected')});
				}
			} else {
				data_class = classes;
				if($('.calls-config td.active:not(.c0)').length > 0){
					call_class = $.grep($('.calls-config td.active:not(.c0)').attr('class').split(' '), function(v,i){return (v != 'active' && v != 'basic' && v != 'selected')});
				}
			}
			var selector = '.'+call_class;
			if(data_class){
				selector = selector + '.'+ data_class;
				t.find('.compare-columns .'+data_class).parent().children().removeClass('active');
				t.find('.compare-columns .'+data_class).addClass('active');
			} else {
				selector = selector + '.c1';
			}			
			t.find(selector).parent().children().removeClass('active');
			t.find(selector).addClass('active');
		});
	});
};

/* phone image gallery */
var initPhoneGalleryScroll = function(el){
	var o = this;
	$('.gallery .images').serialScroll({
		items:'li',
		prev:false,
		next:false,
		offset:0, 
		start:true, 
		duration:500,
		force:false,
		step: 1,
		stop:true,
		lock:false,
		cycle:false, 
		jump: false
	});
	$('.gallery .nav li a').click(function(e){
		e.preventDefault();
		moveTo($(this).text() - 1 );
		clearInterval(phoneGalleryInterval);
	});
	
	var keyStep = 0;
	var phoneGalleryInterval = setInterval(function(){autoGalleryMove()},2000);

	var moveTo = function(i){
		$('.gallery .nav li').removeClass('active');
		$('.gallery .nav li').eq(i).addClass('active');
		var current_pic = $('.gallery .images li').eq(i).find('a').attr('href');
		$('.gallery .zoom').attr('href',current_pic);
		$('.gallery .images').trigger('goto', i);
	};

	var autoGalleryMove = function(){
		if(keyStep == $('.gallery .images li').length){
//			clearInterval(phoneGalleryInterval);
//			return false;
			keyStep = 0;
			moveTo(keyStep);
		}
		moveTo(keyStep);
		keyStep += 1;
	}
	
};

var phoneFilterStats = function(){
	var w = $('#phone-filters');
	w.find('.make ol li input').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Blagovna znamka', $(this).val());		
		});
	w.find('.make ol li.more .extend-filters').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Prikaži več', 'Vse znamke');		
		});
	w.find('.property ol li input').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Lastnosti', $(this).parent().find('label').text());
		});
	w.find('.property ol li.more .extend-filters').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Prikaži več', 'Vse lastnosti');		
		});
	w.find('.phone-type ol li input').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Tip telefona', $(this).parent().find('label').text());
		});
	w.find('.part3 fieldset .more').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Prikaži več', 'Več možnosti');		
		});
	w.find('#pkgs1-radio').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Paket', 'Poslovni');
		});
	w.find('#pkgs2-radio').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Paket', 'Zame');
		});
	w.find('#pkgs3-radio').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Paket', 'Orto');
		});
	w.find('#pkgs4-radio').click(function(){
		pageTracker._trackEvent('telefoni', 'filter - Paket', 'SIMPL');		
		});
	w.find('.sorting .price-sort').click(function(){
		pageTracker._trackEvent('telefoni', 'sort - Cena', '');
		});
	w.find('.sorting .make-sort').click(function(){
		pageTracker._trackEvent('telefoni', 'sort - Blagovna znamka', '');		
		});
};

var phoneStats = function(){
	var cat = 'telefon';
	if($('body').hasClass('business')){
		cat = 'telefon_poslovni';
	}
	var w = $('.the-phone').parent();
	w.find('.the-phone .gallery .images ul li a').click(function(){
		pageTracker._trackEvent(cat, 'telefon - zoom', 'slika');
	});
	w.find('.the-phone .gallery .zoom').click(function(){
		pageTracker._trackEvent(cat, 'telefon - zoom', 'ikona');
	});
	w.find('.the-phone .gallery .nav li').click(function(){
		pageTracker._trackEvent(cat, 'telefon - naprej-nazaj', 'on page');
	});
	w.find('.the-phone .data .to-favorite').click(function(){
		pageTracker._trackEvent(cat, 'telefon - v izbor', '');
	});
	w.find('.the-phone .properties h4 .expand-properties').click(function(){
		pageTracker._trackEvent(cat, 'telefon - podrobneje', '');
	});
	if('telefon_poslovni' == cat){
		w.find('.tab-content .pricing td.basic').click(function(){
			var pack = $(this).parent().find('td:first div strong a').text();
			var upgrade = $(this).parent().parent().find('tr:first td.active div strong').text();
			pageTracker._trackEvent(cat, 'konfigurator - ', pack + ' - ' + upgrade);
		});
	} else {
		w.find('.tab-content .pricing td.basic').click(function(){
			var pack = $(this).parent().find('td:first div strong a').text();
			var upgrade = $(this).parent().parent().find('tr:first td.active div strong').text();
			var type = w.find('.tabs .active a strong').text();
			pageTracker._trackEvent(cat, 'konfigurator - ' + type, pack + ' - ' + upgrade);
		});
	}
	if(w.find('.sms-config').exists()){
		w.find('.sms-config td.basic').click(function(){
			var pack = w.find('.tab-content .pricing td.c0.active div strong a').text();
			var upgrade = w.find('.tab-content .pricing .r1 .active div strong').text();
			var smsUpgr = $(this).find('div strong').text();			
			pageTracker._trackEvent(cat, 'konfigurator - SMS' , pack + ' - ' + upgrade + ' - '+ smsUpgr);
		});
	}
};

var packageStats = function(){
	var cat = 'Zame';
	var w = $('.package-wrap');
	if($('body').hasClass('business')){
		cat = 'Poslovni';
	} else {
		cat = w.find('.js_cat').text(); 
	}
	w.find('.calls-config td.basic').click(function(){
		pageTracker._trackEvent('paketi_'+cat, 'konfigurator - pogovor', $(this).find('div strong').text());		
	});
	w.find('.data-config td.basic').click(function(){
		pageTracker._trackEvent('paketi_'+cat, 'konfigurator - prenos_podatkov', $(this).find('div strong').eq(0).text());
	});
	w.find('.sms-config td.basic').click(function(){
		pageTracker._trackEvent('paketi_'+cat, 'konfigurator - sporočila', $(this).find('div strong').eq(0).text());
	});
	w.find('.tabs .p1 a').click(function(){
		pageTracker._trackEvent('paketi_'+cat, 'telefoni', 'priporočen');
	});
	w.find('.tabs .p2 a').click(function(){
		pageTracker._trackEvent('paketi_'+cat, 'telefoni', 'moj izbor');
	});
	w.find('.fw-button').click(function(){
		pageTracker._trackEvent('paketi_'+cat, 'telefoni', 'vsi telefoni');
	});
	w.find('.compare-content ol h4 a').click(function(){
		pageTracker._trackEvent('paketi_'+cat, 'kateri telefon', $(this).text());
	});
};

var squeezePackageTable = function() {
	if ($('.package-wrap .js_cat2').text() == 'tujina-zame') {
		if ($('table.calls-config td.basic').length == 6) $('table.calls-config td.basic div').css('width', '79px').filter('.tujina').css('width', '94px');
	} else if ($('.package-wrap .js_cat2').text() == 'tujina-podjetni') {
		if ($('table.calls-config td.basic').length == 6) $('table.calls-config td.basic div').css('width', '81px');
	}
};
