//LOGIN FORM
$(document).ready(function() {
var $elems = $('#login-form label, #login-form input, #login-form button.login-submit, #login-form #login-form-close');
	$('#login-button.login').click(function(e) {
		e.preventDefault();
		$elems.hide();
		$('#login-form').css('width', '0px').show().animate(
			{width: '565px'},
			400,
			function() { 
				if ($.browser.msie) { $elems.show(); }
				else { $elems.fadeIn(); }
			}
			);
	});
	$('#login-form-close').click(function() {
		if ($.browser.msie) { $elems.hide(); }
				else { $elems.fadeOut(); }
		$('#login-form').animate(
			{width: '0px'},
			400,
			function() { 
				$('#login-form').hide();
			}
			);
	});
});

//FOOTER social networking links
$(document).ready(function() {
	$('#social-networking li').hover(function() {
		$(this).fadeTo(200, 0.5);
	},
	function() {
		$(this).stop(true, true).fadeTo(100, 1);
	}
	);
});

//HOME-SLIDESHOW
$(window).load(function() {
	$('#home-slideshow .slide .txt-overlay').show();
    $('#home-slideshow').cycle({
		fx: 'fade',
		timeout: 4000,
		pager: '#nav',
		pause: 1
	});
});


//jCarousel HOME
$(window).load(function() {
	$(".carousel ul").slideDown();
	var maxH = 0;
	$('.carousel ul li').each(function() {
		var currH = $(this).height();
		if (currH > maxH) { maxH = currH; }
	});
	$('#offer-selection .next, #offer-selection .prev').height(maxH);
	if ($('.carousel ul li').length > 4) {
		$(".carousel").jCarouselLite({
		   btnNext: ".next",
		   btnPrev: ".prev",
		   visible: 4,
		   scroll: 1,
		   auto: 10000,
		   speed: 500,
		   hoverPause:true,
		   circular: true
		});
	}
	else {
		$('#offer-selection .next, #offer-selection .prev').css({background: '#ffffff', cursor: 'default' });
		$('.carousel ul li').height(maxH).css('overflow', 'hidden');
	}
});

//HOME tabs-text-box
$(document).ready(function() {
  $(".big-text-box .titles li a").live("click", function(e){ 
    e.preventDefault();
	$(this).parent().addClass('active').siblings().removeClass('active');
    $.ajax({
     type: "GET",
     url: $(this).attr('rel'),
     success: function(data){
		$(".big-text-box .text .inner").fadeOut(100).html(data).fadeIn(300);
     }
  });
  });
});

//LIGHTBOX
$(document).ready(function() {
    $(".lightbox").lightBox();
    $(".lightbox-2").each(function() {
		var currRel = $(this).attr('rel');
		$('.lightbox-2[rel=' + currRel + ']').lightBox();
	});
    $(".lightbox-3").each(function() {
		var currRel = $(this).attr('rel');
		$('.lightbox-3[rel=' + currRel + ']').lightBox();
	});
	if (!($.browser.msie)) {
		$('#lightbox-nav-btnPrev').live('mouseenter', function() {
			$(this).css('opacity', '1'); 
			})
			.live('mouseleave', function() {
			$(this).css('opacity', '0'); 
			});
		$('#lightbox-nav-btnNext').live('mouseenter', function() {
			$(this).css('opacity', '1'); 
			})
			.live('mouseleave', function() {
			$(this).css('opacity', '0'); 
			});
	}	
});

//SELECTBOX
$(document).ready(function() {
	$(".selectbox").selectbox();
	$('.control-box.bottom .jquery-selectbox-list').css('top', (-($('.control-box.bottom .jquery-selectbox-list').height()+6)) + 'px');
	$('.delivery_select .jquery-selectbox-list').css('top', (-($('.delivery_select .jquery-selectbox-list').height()+6)) + 'px');
});

//UPLOAD FILE REPLACEMENT
$(document).ready(function() {
    if($("#file_wrap").html()!=null){
        var pos = $("#file_wrap").offset();
        $("#file_wrap").mousemove(function(e){
          $("#file_input").css({
            top: (e.pageY-pos.top-$('#file_input').height()+10) +"px",
            left: (e.pageX-pos.left-$('#file_input').width()+55) + "px"
          });
        });
        var so = document.getElementById("file_input");
        so.onchange = so.onmouseout = function(){
        	var inputValue = this.value;
        	$("#dummy_input").val(inputValue);
        }
    }
});

//WEBSHOP DETAIL PHOTO GALLERY
$(window).load(function() {
	$('div#webshop-photos li .img-large img').each(function() {
		var Hght = $(this).height();
		if (Hght < 230) {
			$(this).css('margin-top', (230-Hght)/2);
		}
		if (Hght > 230) {
			$(this).css('margin-top', (230-Hght)/2);
		}
	});
	if (!($.browser.msie)) {
		$('div#webshop-photos li .img-large').fadeTo(200, 1);
	}
});
$(document).ready(function() {
	if (!($.browser.msie)) {
		$('div#webshop-photos li .img-large').css('opacity', 0);
	}
	$('div#webshop-photos li:first').addClass('on');
	$('div#webshop-photos a.thumb').hover(
	function() {
		$(this).parent().addClass('on').siblings().removeClass('on');
	}
	);
	$('div#webshop-photos a.thumb').click(function(e) {e.preventDefault();});
});

//WEBSHOP DROPDOWN
$(document).ready(function() {   
    $(".webshop-detail-wrapper div.select-version ul").before('<div class="rpl-sel"><span>'+$(".webshop-detail-wrapper div.select-version ul .active").text()+'</span></div>');
    $(".webshop-detail-wrapper div.select-version .rpl-sel").click(function(){
        if($("#rpl-sel-drop").is(":visible")){ 
            $("#rpl-sel-drop").slideUp(function(){
                $(this).remove();
            });
        }
        var offset = $(this).offset();
        var offsetTop = offset.top+$(this).height()-1;
        $("body").append('<ul id="rpl-sel-drop">'+$(".webshop-detail-wrapper div.select-version ul").html()+'</ul>');
        $("#rpl-sel-drop").css({top:offsetTop, left:offset.left}).slideDown();
    });
});

//PRODUCTION GRID HEIGHT
$(window).load(function() {
	$('#production-grid .row').each(function() {
		var rowH = ($(this).height()) - 67;
		$(this).children('.production-item').children('.inner').height(rowH);
		$(this).children('.production-item:last').addClass('last');
	});
	
});

//GMAP
var map;
var map_infowindow;
var map_markers = new Array;
$(document).ready(function(){
    if( $('#gmap').length > 0 ){
            map = new google.maps.Map(document.getElementById("gmap"), {
                zoom: 9,
				mapTypeControlOptions: {
				  mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID ]
				},
				navigationControlOptions: {style: google.maps.NavigationControlStyle.DEFAULT},
                center: new google.maps.LatLng( 46.043013, 15.994499 ),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
            map_infowindow = new google.maps.InfoWindow({
                content: 'map_infowindow'
            });
    }
});

//LOCATION MAP POPUP
$(document).ready(function() {
    $("#location-map ul a,#location-map ul span").click(function(){
        $("#location-popup").remove();
        var data="<strong>"+$(this).parents("li:first").attr("title")+"</strong>";
        $(this).parents("li:first").find("dl").each(function(){
            data = data+"<b>"+$(this).find("dt").text()+"</b>";
            $(this).find("dd").each(function(){
                data = data+$(this).html()+"<br />";
            });
        });
        $("body").append('<div id="location-popup"><div class="bkg-1"><div class="bkg-2"><img class="close" src="images/close.gif" alt="zatvori" title="zatvori" />'+data+'</div></div></div>');
        var offset = $(this).parents("li:first").find("span").offset();       
        $("#location-popup .close").click(function(){ $("#location-popup").remove(); });
        
        $("#location-popup").css({left: offset.left - 105, top: offset.top-$("#location-popup").height()-10 }).show();
    });
});

//CLEAR-DEFAULT TEXT
$(document).ready(function() {
	$('input[type=text]').each(function() {
		var defaultTitle = $(this).attr('title');
		if ($(this).attr('value') == defaultTitle) {
			$(this).css('font-style', 'italic');
		}
		if ($(this).attr('value') == '') {
			$(this).attr('value', defaultTitle).css('font-style', 'italic');
		}
		$(this).focus(function() {
			if ($(this).attr('value') == defaultTitle) {
				$(this).attr('value', '').css('font-style', 'normal');
			}
		});
		$(this).blur(function() {
			if ($(this).attr('value') == '') {
				$(this).attr('value', defaultTitle).css('font-style', 'italic');
			}
		});
	});
	$('form').has('input[type=text][title]').submit(function() {
		$(this).find('input[type=text][title]').each(function() {
			var defaultTitle = $(this).attr('title');
			if ($(this).attr('value') == defaultTitle) {
				$(this).attr('value', '');
			}
		});
	});
});

//ORDER FORM
function r1() {
	if(!($('fieldset.company label.r1 input:checked').val())) {
		$('fieldset.company span.red-star').css('visibility', 'hidden');
	}
	if($('fieldset.company label.r1 input:checked').val()) {
		$('fieldset.company span.red-star').css('visibility', 'visible');
	}
}
$(document).ready(function() {
	r1();
	$('fieldset.company label.r1 input').change(function() {
		r1();
	});
	
	if ($('select[name=delivery_id]').length != 0) {
		var no_error_msg = true;
		$('select[name=delivery_id]').closest('form').submit(function(e) {
			if ($('select[name=delivery_id]').val().length == 0) {
				e.preventDefault();
				if (no_error_msg == true) {
					$('div.delivery_select div.jquery-selectbox').after('<div class="delivery_error">' + phrasebook.lbl_delivery_error + '</div>')
					no_error_msg = false;
				}
			}
		});
	}
});



//SHARE TOOLBAR
$(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);
          switch ($(this).attr("rel")){
              case "mail":
                  window.location = "mailto:?subject="+pageTitle+"&body="+pageLink;
                  $(".dimedia-toolbar-button").click();
                  break;
              case "twitter":
                  var link = $(this).attr("href").split("|");
                  window.open(link[0]+pageTitle+link[1]+pageLink,"_blank");
                  $(".dimedia-toolbar-button").click();
                  return false;
                  break;
              case "technorati":
                  var link = $(this).attr("href").split("|");
                  window.open(link[0]+pageLink,"_blank");                  
                  $(".dimedia-toolbar-button").click();
                  return false;
                  break;
              default:
                  var link = $(this).attr("href").split("|");
                  window.open(link[0]+pageLink+link[1]+pageTitle,"_blank");
                  $(".dimedia-toolbar-button").click();
                  return false;
                  break;
          }
      }
  });
  
  $(".dimedia-toolbar-button").toggle(function(){
      var dimediaToolbarPosition = $(".dimedia-toolbar-button").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"});
      }
  },function(){
      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").removeClass("on");    
        });
      }
  });
});

