//HEADER SLIDESHOW
$(document).ready(function(){
	$.get(phrasebook.URL_ROOT+'/simple.php?page=xml_home_slideshow', function(data) {
		$(data).find('image').each(function(){
			$('.slideshow .img-wrapper').append('<div class="slide"><a href="'+$(this).attr('link')+'" target="'+$(this).attr('target')+'"><img src="'+$(this).attr('url')+'" /></a></div>');
		});
		$('.slideshow .img-wrapper .slide a[href=""]').css({'cursor':'default'}).click(function(e){
			e.preventDefault();
		});
		$('.slideshow .img-wrapper').cycle({
			fx: 'fade',
			timeout: 4000,
			pager: '.slideshow .slide-buttons'
		});
	}, 'xml');
});

$(function(){

  /* FORM SUBMIT  */
  $('#filterVehicles form input[type="submit"]').hide(); 
  //$('#filterVehicles form').change(function(){ this.submit(); }); 
  $('#filterVehicles select').sSelect().change(function(){
	
	
	
	this.form.submit();
  });
  $("#filterVehicles input").custCheckBox({callback: function(){ $('#filterVehicles form').submit();}});
  
}); 

$(document).ready(function(){
  $(".scrollable").scrollable({  });
  
  $(".cl").click(function(){alert("Trigger");});
  
  
  $(".menu_main > li > a").each(function(){
    var lines = parseInt($(this).find("span").height())/parseInt($(this).find("span").css("line-height"));
    if(lines < 1.5) 
      $(this).css({"height": "9px", "padding": "13px 2px"});
    //if(lines = 1) $(this).find("a").css({"padding-top": "12px", "height": "23px"});
  });
  
  Cufon.replace('.cufon, .menu_main span');
    
});

//LIGHTBOX
$(document).ready(function() {
    $(".lightbox").lightBox();
});


//GALLERY PHOTO SLIDER
function animateScroll(currImg, posLeft) {
	$('.model-image-gallery li a[rel="img' + currImg + '"], ul.gallery-thumbs li a[rel="img' + currImg + '"]').parent().addClass('active');
	$('.model-image-gallery li a[rel!="img' + currImg + '"], ul.gallery-thumbs li a[rel!="img' + currImg + '"]').parent().removeClass('active');
	$('.model-image-gallery').animate(
			{
			left: posLeft
			},
			500
			);
	$('.download-share .inner').html($('.model-image-gallery li.active .hidden-content').html());
}

$(document).ready(function() {
	var currImg = '0';
	var posLeft = '0';
	var imgCount = $('ul.gallery-thumbs li').length;
	var containerWidth = imgCount * 877;
	$('.model-image-gallery').width(containerWidth + 'px');
	animateScroll(currImg, posLeft);
	
	$('.model-image a.next').click(function(e) {
	e.preventDefault();
	$('.model-image-gallery').stop(true, true);
	var currLeft = parseInt($('.model-image-gallery').css('left').replace("px",""));
	if (currLeft > -(containerWidth - 877)) {
		var currImg = (currLeft / -877) + 1;
		var posLeft = '-=877';
		animateScroll(currImg, posLeft);
		}
	else {
		var currImg = 0;
		var posLeft = '0';
		animateScroll(currImg, posLeft);
		}
	});
	$('.model-image a.prev').click(function(e) {
	e.preventDefault();
	$('.model-image-gallery').stop(true, true);
	var currLeft = parseInt($('.model-image-gallery').css('left').replace("px",""));
	if (currLeft < 0) {
		var currImg = (currLeft / -877) - 1;
		var posLeft = '+=877';
		animateScroll(currImg, posLeft);
		}
	else {
		var currImg = imgCount - 1;
		var posLeft = -(containerWidth - 877);
		animateScroll(currImg, posLeft);
		}
	});
	
	$('ul.gallery-thumbs li a').click(function(e) {
		e.preventDefault();
		var currImg = parseInt($(this).attr('rel').replace("img",""));
		var posLeft = -(currImg * 877) + 'px';
		animateScroll(currImg, posLeft);	
	});
});

//GALERIJA download+share
$(document).ready(function() {
	$('div.download-share a.download').live('click', function() {
		$(this).attr('href', $('div.download-share select').val());
	});

});

$(window).load(function(){
	var maxH = 0;
	$(".fixHeight01").each(function(){
		if($(this).height() > maxH) maxH = $(this).height();
	});
	$(".inner").css("min-height", maxH);
	
});



//POPUP LEGAL

function launchPopupWindow() {
		window.open(url_root+"izjava_o_privatnosti.html", "", "width=580,height=500,scrollbars=yes");
		return false;
	};
$(document).ready(function(){
	$('a.popup-legal').click(function(e) {
		e.preventDefault();
		launchPopupWindow();
	});
});

//SHARE TOOLBAR (!!MODIFIED for Opel!!)

$(document).ready(function() {
  $("body").append('<div id="dimedia-toolbar"><div class="bottom-bkg"><div class="middle-bkg"><ul><li><a href="http://www.facebook.com/share.php?u=|&t=" rel="facebook" title="facebook"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon1.gif" alt="facebook" /></a></li><li><a href="http://twitter.com/home?status=|%20-%20" rel="twitter" title="twitter"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon2.gif" alt="twitter" /></a></li><li><a href="http://digg.com/submit?phase=2&url=|&title=" rel="digg" title="digg"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon3.gif" alt="digg" /></a></li><li><a href="http://delicious.com/post?url=|&title=" rel="delicious" title="delicious"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon4.gif" alt="delicious" /></a></li><li><a href="http://www.myspace.com/Modules/PostTo/Pages/?u=|&t=" rel="myspace" title="myspace"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon5.gif" alt="myspace" /></a></li><li><a href="http://www.stumbleupon.com/submit?url=|&title=" rel="stumbleupon" title="stumbleupon"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon6.gif" alt="stumbleupon" /></a></li><li><a href="http://www.google.com/bookmarks/mark?op=edit&bkmk=|&title=" rel="google" title="google bookmarks"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon7.gif" alt="google bookmarks" /></a></li><li><a href="http://bookmarks.yahoo.com/toolbar/savebm?opener=bm&ei=UTF-8&u=|&t=" rel="yahoo" title="yahoo bookmarks"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon8.gif" alt="yahoo bookmarks" /></a></li><li><a href="http://www.linkedin.com/shareArticle?mini=true&url=|&title=" rel="linkedin" title="linkedin"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon9.gif" alt="linkedin" /></a></li><li><a href="https://favorites.live.com/quickadd.aspx?marklet=1&url=|&title=" rel="live" title="live"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon10.gif" alt="live" /></a></li><li><a href="http://technorati.com/faves?add=|" rel="technorati" title="technorati"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon11.gif" alt="technorati" /></a></li><li><a rel="mail" title="e-mail"><img src="'+url_root+'/images/toolbar/dimedia-toolbar-icon12.gif" alt="e-mail" /></a></li></ul></div></div></div>');
  
  $("#dimedia-toolbar a").click(function(){
      if(!($(this).attr("rel")==null||$(this).attr("rel")=="")){
          var pageTitle = encodeURI(document.title.replace(/š/g,"s").replace(/đ/g,"d").replace(/č/g,"c").replace(/ć/g,"c").replace(/ž/g,"z").replace(/Š/g,"S").replace(/Đ/g,"D").replace(/Č/g,"C").replace(/Ć/g,"C").replace(/Ž/g,"Z"));
          var pageLink = encodeURI(window.location);
		  if ($('.dimedia-toolbar-button-2').hasClass('on')) {
			var pageLink = $('div.download-share select').val();
		  }
          switch ($(this).attr("rel")){
              case "mail":
                  window.location = "mailto:?subject="+pageTitle+"&body="+pageLink;
                  $(".dimedia-toolbar-button, .dimedia-toolbar-button-2").click();
                  break;
              case "twitter":
                  var link = $(this).attr("href").split("|");
                  window.open(link[0]+pageTitle+link[1]+pageLink,"_blank");
                  $(".dimedia-toolbar-button, .dimedia-toolbar-button-2").click();
                  return false;
                  break;
              case "technorati":
                  var link = $(this).attr("href").split("|");
                  window.open(link[0]+pageLink,"_blank");                  
                  $(".dimedia-toolbar-button, .dimedia-toolbar-button-2").click();
                  return false;
                  break;
              default:
                  var link = $(this).attr("href").split("|");
                  window.open(link[0]+pageLink+link[1]+pageTitle,"_blank");
                  $(".dimedia-toolbar-button, .dimedia-toolbar-button-2").click();
                  return false;
                  break;
          }
      }
  });

  $(".dimedia-toolbar-button, .dimedia-toolbar-button-2").live('click', function(e) {
  e.preventDefault();

  if (!$(this).hasClass('on')) {
	  if ($(this).hasClass('dimedia-toolbar-button')) {
		$(".dimedia-toolbar-button-2").removeClass('on');
		}
      var dimediaToolbarPosition = $(this).offset();
      var dimediaToolbarPositionTop = dimediaToolbarPosition.top-80;
      var dimediaToolbarPositionLeft = dimediaToolbarPosition.left;
      $("#dimedia-toolbar").css({top:dimediaToolbarPositionTop,left:dimediaToolbarPositionLeft});
      
      $(this).addClass("on");
      if ($.browser.msie && $.browser.version==6) {
        $("#dimedia-toolbar").css({marginTop:"-10px"}).show();
      } else {
        $("#dimedia-toolbar").css("display","block").animate({marginTop:"-10px",opacity:"1"});
      }
	  
	  return;
	}
  if ($(this).hasClass("on")) {
      if ($.browser.msie && $.browser.version==6) {
        $("#dimedia-toolbar").hide();
      } else {
        $("#dimedia-toolbar").animate({marginTop:"0px",opacity:"0"},function(){
            $(this).css("display","none");
            $(".dimedia-toolbar-button, .dimedia-toolbar-button-2").removeClass("on");    
        });
      }
	}
  });
});


// KOREKCIJA PUTANJA ZA SLIKE IZ CMS-a NA PRIVREMENOJ ADRESI (radi samo u ff)
/*
$(document).ready(function() {
	$('.cms-txt img[src^="\\/"]').each(function() {
		var getSrc = $(this).attr('src');
		$(this).attr('src', 'http://www.dimedia.hr/test/opel' + getSrc );
	});
});
*/

//ADD CLASSES FOR TABLES
$(document).ready(function() {
	$('table.t01 tr:odd, table.t02 tr:odd, table.t03 tr:odd').addClass('odd');
	$('table.t01 tr:even, table.t02 tr:even, table.t03 tr:even').addClass('even');
	$('table.t01 tr:first, table.t02 tr:first, table.t03 tr:first').addClass('first');
	$('table.t01 tr:last, table.t02 tr:last, table.t03 tr:last').addClass('last');
});

//DEACTIVATE BANNER LINKS WITHOUT href ATTR
$(document).ready(function() {
	$('a.banner[href="#"], a.banner-home[href="#"]').css('cursor', 'default').click(function(e) {
		e.preventDefault();
	});
});

/* CONTACT POPUP */
var popupStatus = 0;

function loadPopup(){
	if(popupStatus==0){
		$('embed, select, object').css({'visibility':'hidden'});
		$("#backgroundPopup").css({
			"opacity": "0.8"
		});
		$("#backgroundPopup").show();
		$("#popupContact").appendTo('body').show();
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
			$("html").scrollTop((windowHeight/2-popupHeight/2)-100); 
	
}


//FLOATING BANNER HOME
$(document).ready(function(){
	$('#floating-banner .close').bind('click',function(e){
		e.preventDefault();
		$(this).closest('#floating-banner').hide();
	});
});
