jQuery.noConflict();

jQuery(function(){

/* image popup */

jQuery("#display-gallery").dialog({
			autoOpen: false, 
			width: 720,
			modal: true,
			close: function() {
				jQuery("ul.specificatii").show();
			}
			});
			
jQuery(window).resize(function() {
	jQuery(".productImages").height(jQuery(window).height()-10);
	});

var dialogH = jQuery(window).height()-10;
jQuery(".productImages").height(dialogH);

var containerW = 720;

/* image mousemove position */
function changeImagePos(fullWidth, fullHeight, thumbnailWidth, thumbnailHeight) {
jQuery('#display-gallery').mousemove(function(e) {
	
	var imageW = jQuery(".productImagesItems .active img").width();
	var imageH = jQuery(".productImagesItems .active img").height();
	
	if (imageW >containerW || imageH > thumbnailHeight) {
		var offset = jQuery(this).offset();
		var mouseX = e.pageX - offset.left; 
		var mouseY = e.pageY - offset.top; 
	
		var posX = (Math.round((mouseX/thumbnailWidth)*100)/100) * (fullWidth-thumbnailWidth);
		var posY = (Math.round((mouseY/thumbnailHeight)*100)/100) * (fullHeight-thumbnailHeight);
	
		jQuery('.productImagesItems .active img').css({
			'left': '-' + posX + 'px',
			'top': '-' + posY + 'px'
		});
	}

});
}

function init_small_imgs(imageW) {
	if (imageW < containerW) {
		jQuery(".productImagesItems div.active img").css({
			"left": "50%",
			"margin-left": -imageW/2
		});
	}

}

function getTotalImages() {
	var count = 0;
	jQuery(".productImagesItems > div").each(function() {
		count++;		
	});
	return count;
}

function setImageActive(index) {
	jQuery('.productImagesItems > div').each(function(i) {
		jQuery(this).removeClass("active");
		if (i==index) { jQuery(this).addClass("active"); }
	});
	var imageW = jQuery(".productImagesItems .active img").width();
	// init small imgs
	init_small_imgs(imageW);
}

function setThumbActive(index) {
	jQuery('div.thumbs .gallery > li').each(function(i) {
		jQuery(this).removeClass("curent");
		if (i==index) { jQuery(this).addClass("curent"); }
	});
	jQuery('.produs-imag .produs-thumbs > li').each(function(i) {
		jQuery(this).removeClass("curent");
		if (i==index) { jQuery(this).addClass("curent"); }
	});
}

// get image size
function getImageSize(id) {
	var newImg = new Image();
	newImg.src = jQuery(".item"+id).attr("src");
	return newImg.height;
}

// init gallery
function init_gallery(index) {
		changeDisplayImage(index-1, 0)
}

jQuery(".produs-big-imag").live("click", function() {
	jQuery("ul.specificatii").hide();
	jQuery("#display-gallery").dialog("open").css({"top":5});
	jQuery("#display-gallery").prev().hide();
	var index = jQuery(".produs-imag .produs-thumbs > li.curent").attr("item");
	
	init_gallery(index);
	return false;
});

jQuery(".gallery-close-btn").live("click", function() {
	jQuery("#display-gallery").dialog("close");
	return false;
});

jQuery(".gallery-nav-next").live("click", function() {
	var totalImgs = getTotalImages();
	var curIndex = jQuery(".productImagesItems .active").attr("item");
	
	if (totalImgs == curIndex) {
		curIndex = 0;
	}
	
	changeDisplayImage(curIndex, 0);
	jQuery(".zoomer").removeClass("trig");
	return false;
	});
	
jQuery(".gallery-nav-prev").live("click", function() {
	var totalImgs = getTotalImages();
	var curIndex = jQuery(".productImagesItems .active").attr("item");
	
	if (curIndex == 1) {
		curIndex = parseInt(totalImgs) + 1;
	}
	
	
	changeDisplayImage(curIndex-2, 0);
	jQuery(".zoomer").removeClass("trig");
	return false;
	});

function changeDisplayImage(index, zoomer) {
	
	containerH = jQuery(window).height()-10;
	
	if (containerH > 870) {
		containerH = 870;
	}
	
	var leftPos = index * containerW;
	jQuery(".productImagesItems").css({"left":-leftPos});
	setImageActive(index);
	setThumbActive(index);
	var imageW = jQuery(".productImagesItems .active img").width();
	var imageH = jQuery(".productImagesItems .active img").height();
	
	
	// reset
	jQuery('.productImagesItems div img').css({
		'left':0,
		'top': 0
	});
	
	imageH = getImageSize(index);

	
	if (imageW>=containerW) {
		if (!zoomer) {
		// zoom reset
		jQuery('.productImagesItems div.active img').css({
			'width':containerW
		});
		imageH = jQuery(".productImagesItems div.active img").height();
		if (imageH >= containerH )
			imageH = jQuery(".productImagesItems div.active img").height();
		}
			else {
					jQuery('.productImagesItems div.active img').css({
						'width':"auto"
					});
					imageH = jQuery(".productImagesItems div.active img").height();
	
			}
	}
	
	var imageW = jQuery(".productImagesItems .active img").width();
	
	// init small imgs
	init_small_imgs(imageW);
	
	
	
	changeImagePos(imageW,imageH,containerW,containerH);	
}

jQuery(".zoomer").live("click", function() {
	var curIndex = jQuery(".productImagesItems .active").attr("item");
	if (jQuery(this).hasClass("trig")) {
		changeDisplayImage(curIndex-1, 0);
		jQuery(this).removeClass("trig");
	}
	else {
		changeDisplayImage(curIndex-1, 0);
		jQuery(this).addClass("trig");
	}
});

jQuery(".produs-thumbs a").live("click", function() {
	var index = jQuery(this).parent().attr("item");
	jQuery(".zoomer").removeClass("trig");
	changeDisplayImage(index-1, 0);
});

/* end image popup */

/* categ image toggle */
jQuery(".product-image").hover(function() {
	jQuery(".aligner", this).fadeOut({
		duration: 100,
		queue:false
		});
	
}, function() {
	jQuery(".aligner", this).fadeIn({
		duration: 50,
		queue: false
		});
});

/* forget passwd slide */
jQuery("#change_password").live("click", function() {
	jQuery("#change-passwd-block").slideToggle("fast");
});

// breadcrumbs

jQuery(".breadcrumb-list li:last-child").addClass("current");

// logo fading
jQuery('#logo').hover(function() {
					// on mouse over
					jQuery(this).find(".hover").stop().fadeTo('fast', 1);
					
					}, 
					function() {
					// on mouse out
					jQuery(this).find(".hover").stop().fadeTo('fast', 0);
					});
// end logo fading

//nav fade init
function init_main_nav() {
	jQuery(".main_nav > li").each(function() {
		$link = jQuery(this).find("a");
		var link_hoverer = '<a href="'+$link.attr("href")+'" class="hover">'+$link.html()+'</a>';
		$link.after(link_hoverer);
	});
}

init_main_nav();

// nav fading
jQuery('.main_nav li').hover(function() {
								// on mouse over
								jQuery(this).find('.hover').stop().fadeTo('fast', 1);
								}, 
								function() {
								// on mouse out
								jQuery(this).find('.hover').stop().fadeTo('fast', 0);
								});

// end fading

//side-banners fading
jQuery(".side-banners li a").hover(function () {
		jQuery(this).addClass("hover");
		jQuery('.side-banners li a').not('.side-banners li a.hover').stop().animate({
				opacity: 0.7
		}, {
				duration: 200
		});
}, function () {
		jQuery(this).removeClass("hover")
		jQuery('.side-banners li a').stop().animate({
				opacity: 1
		}, {
				duration: 200
		});
});

// end side-banners fading

//completeaza tinuta fading
jQuery("#completeaza-tinuta li a").hover(function () {
		jQuery(this).addClass("hover");
		jQuery('#completeaza-tinuta li a').not('#completeaza-tinuta li a.hover').stop().animate({
				opacity: 0.6
		}, {
				duration: 200
		});
}, function () {
		jQuery(this).removeClass("hover")
		jQuery('#completeaza-tinuta li a').stop().animate({
				opacity: 1
		}, {
				duration: 200
		});
});

// end side-banners fading

//produse asemanatoarefading
jQuery("#produse-asemanatoare li a").hover(function () {
		jQuery(this).addClass("hover");
		jQuery('#produse-asemanatoare li a').not('#produse-asemanatoare li a.hover').stop().animate({
				opacity: 0.6
		}, {
				duration: 200
		});
}, function () {
		jQuery(this).removeClass("hover")
		jQuery('#produse-asemanatoare li a').stop().animate({
				opacity: 1
		}, {
				duration: 200
		});
});

// end side-banners fading

// Pagina produs zoom
/*jQuery(".jqzoom").jqueryzoom({
				xzoom: 410, //zooming div default width(default width value is 200)
				yzoom: 410, //zooming div default width(default height value is 200)
				offset: 10, //zooming div default offset(default offset value is 10)
				position: "right", //zooming div position(default position value is "right")
 				preload:1,
				lens:1
			});

jQuery('.jqzoom').hover(function() {
								jQuery('.zoom-overlay').css({"display":"block"});
								jQuery('.zoom-overlay').stop().fadeTo("normal",0.8, function(){ 
																				jQuery(".zoomdiv").fadeTo("normal", 1);
																			});
								jQuery('ul.specificatii').css({"visibility":"hidden"});
							}, function() { 
								jQuery('.zoom-overlay').stop().fadeTo("normal",0, function() {
																				jQuery('.zoom-overlay').css({"display":"none"});
																			});
								jQuery('ul.specificatii').css({"visibility":"visible"});
								
							});*/
// end pagina produs zoom

//inputur	Search
jQuery('.search-input').focus(function(){ 
	if(jQuery(this).val() == jQuery(this).attr('defaultValue'))
	{
		jQuery(this).val('');
	}
});

jQuery('.search-input').blur(function(){
	if(jQuery(this).val() == '')
	{
		jQuery(this).val(jQuery(this).attr('defaultValue'));
	} 
});

//inputur	Newsletter
jQuery('.news-box input[type=text]').focus(function(){ 
	if(jQuery(this).val() == jQuery(this).attr('defaultValue'))
	{
		jQuery(this).val('');
	}
});

jQuery('.news-box input[type=text]').blur(function(){
	if(jQuery(this).val() == '')
	{
		jQuery(this).val(jQuery(this).attr('defaultValue'));
	} 
});

// end inputr newsletter

// Tabs
jQuery('.tabsWrapper').tabs();


// tabs for pg_produs
jQuery('.tabsWrapper').tabs();

// dialogs
  // for tabel-marimi
  jQuery('#display-tabel-marimi').dialog({autoOpen: false, width: 740, modal: true});
  jQuery('.tabel-marimi').click(function(){
		jQuery('#display-tabel-marimi').fadeIn(200).dialog('open');
		return false;
	});

  // for newsletter
  jQuery('#display-news-box').dialog({ autoOpen: false, width:475, modal: true });
  jQuery('.news-box .search-submit').click(function() {
		jQuery('#display-news-box').fadeIn(200).dialog('open');
		return false;
	});
  
  // for terms
  jQuery('#display-termeni-conditii').dialog({ autoOpen: false, width:600, modal: true });
  jQuery('#terms').click(function() {
		jQuery('#display-termeni-conditii').fadeIn(200).dialog('open');
		return false;
	});
  
  // for modificare cos
  jQuery('#display-edit-item').dialog({autoOpen: false, width: 212, modal: true});
  jQuery('.edit_item').click(function(){
		jQuery('#display-edit-item').fadeIn(200).dialog('open');
		return false;
	});
  
  // for comanda live
  jQuery('#display-comanda-live').dialog({autoOpen: false, width: 286, modal: true});
  jQuery('.btn_comanda_live').click(function(){
		jQuery('#display-comanda-live').fadeIn(200).dialog('open');
		return false;
	});
	
  // for vezi harta
    jQuery('#display-vezi-harta').dialog({ autoOpen: false, width: 700, modal: true });
    jQuery('.btn_vezi_harta, .btn_vezi_harta_nostyle').click(function () {
        jQuery('#display-vezi-harta').fadeIn(200).dialog('open');
        return false;
    });
	
	// for panoramas
    jQuery('#display-panorama1').dialog({ autoOpen: false, width: 700, modal: true });
    jQuery('.panorama1').click(function () {
        jQuery('#display-panorama1').fadeIn(200).dialog('open');
        return false;
    });
	
	jQuery('#display-panorama2').dialog({ autoOpen: false, width: 700, modal: true });
    jQuery('.panorama2').click(function () {
        jQuery('#display-panorama2').fadeIn(200).dialog('open');
        return false;
    });

   // for checkout
   
  jQuery("#checkout-forgot_passwd").click(function(e) {
	  jQuery("#onepagecheckout_loginbox").hide();
	  jQuery("#onepagecheckout_forgotbox").show();
	  var title = jQuery("#onepagecheckout_forgotbox .page-title span").html();
	  jQuery( "#checkout-login-dialog" ).dialog( "option", "title", title );
	  return false;
  });
  
    jQuery("#checkout-login").click(function(e) {
	  jQuery("#onepagecheckout_loginbox").show();
	  jQuery("#onepagecheckout_forgotbox").hide();
	  var title = jQuery("#onepagecheckout_loginbox .page-title span").html();
	  jQuery( "#checkout-login-dialog" ).dialog( "option", "title", title );
	  return false;
  });
   
  var title = jQuery("#onepagecheckout_loginbox .page-title span").html();
  jQuery( "#checkout-login-dialog" ).dialog( "option", "title", title );
   
  jQuery('#checkout-login-dialog').dialog({autoOpen: false, width: 305, modal: true, title: title});
  jQuery('.onepagecheckout_loginlink').click(function(){
		jQuery('#checkout-login-dialog').fadeIn(200).dialog('open');
		return false;
	});
	
  // for cumparaturi rate
  jQuery(".produs-spec").after('<div id="display-buy-rate"></div>');
  jQuery("#display-buy-rate").load("http://www.cristallini.ro/cumparaturi-in-rate-cristallini #load_content");
  jQuery('#display-buy-rate').dialog({autoOpen: false, width: 740, modal: true});
  jQuery('#rate_buy').click(function(){
		jQuery('#display-buy-rate').fadeIn(200).dialog('open');
		return false;
	});
	
// categ childs hover effects 
jQuery(".child-banners li a").hover(function () {
		jQuery(this).addClass("hover");
		jQuery('.child-banners li a').not('.child-banners li a.hover').stop().animate({
				opacity: 0.8
		}, {
				duration: 200
		});
}, function () {
		jQuery(this).removeClass("hover")
		jQuery('.child-banners li a').stop().animate({
				opacity: 1
		}, {
				duration: 200
		});
});

/* zoom product opacity */
jQuery(".produs-zoom-icon").css({opacity: 0.6});
jQuery(".produs-big-imag").hover(function() {
	jQuery(".produs-zoom-icon").stop().animate({
		opacity: 1
		}, 300);
}, function() {
	jQuery(".produs-zoom-icon").stop().animate({
		opacity: 0.6
		}, 300);
});



// gallery navigator (hoverer)

/* terms fix */
jQuery(".agree label").html('Sunt de acord cu <a href="http://www.cristallini.ro/termeni-conditii" target="_blank" title="Termeni si Conditii">termenii si conditiile</a> Cristallini.ro');


// z-index IE6 fix

/*jQuery('.zoomdiv').bgiframe();
jQuery('.zoomdiv img').bgiframe();
jQuery('.ui-dialog').bgiframe();*/

});


