//var $j = jQuery.noConflict(); // no conflict with Magento Prototype


// $(document).ready(function() { 
// 	$('#page-container .page').filter(':visible').fadeOut(1000,function(){
// 		$('#page-container .page-'+page).eq(0).fadeIn(1000);} );
// 	});
// });




jQuery(document).ready(function() {

/* Mark active link */
	jQuery("#nav li a[href$='"+window.location.pathname+"']").addClass("active");

//hide backbar
	jQuery("li.nav-1-1-1:not(:has(ul))").addClass("hidden");

//--------------------------------------------------------------------------//
//Create js clickable elements
    jQuery(".subcategory-listing .products-grid li.item:has(a)").click(function() {
        window.location = jQuery("a:first", this).attr("href");
    });
    jQuery(".category-products .products-grid li.item .product-info:has(a)").click(function() {
        window.location = jQuery("a:first", this).attr("href");
    });
    jQuery(".mini-products-list .product-details:has(a)").click(function() {
        window.location = jQuery("a:first", this).attr("href");
    });
    jQuery(".mini-products-list .product-info:has(a)").click(function() {
        window.location = jQuery("a:first", this).attr("href");
    });
    jQuery(".skintypes li.item:has(a)").click(function() {
        window.location = jQuery("a:first", this).attr("href");
    });

//--------------------------------------------------------------------------//
//hide top image buttons on homepage until hover
    jQuery('.top-image-container-buttons').css('opacity', '0');
    jQuery('.top-image-container').hover(
        function () {
            jQuery('.top-image-container-buttons').stop().animate({
                opacity: 1
            }, 400);
        }, function () {
            jQuery('.top-image-container-buttons').stop().animate({
                opacity: 0
            }, 400);
        });

//--------------------------------------------------------------------------//
// check if size is selected on configurable products
jQuery('.btn-cart').click(function() {
	if (jQuery('#product-options-wrapper .validation-failed')){
		jQuery('#size-not-selected').fadeIn(500);
	}
});
jQuery('.prices').click(function() {
	jQuery('#size-not-selected').fadeOut(function() {
        jQuery('#size-not-selected').remove();
    });
});

// this is the old checkbox code, removed due to errors
//Set first checkbox to active on page load
	// jQuery("a.prices:first").trigger('onclick');
	
	
	
	// jQuery("select#attribute975 option:nth-child(2)").attr("selected", "selected");
	// jQuery("select#attribute975 option:nth-child(2)").attr("selected", "selected");
	// setSize(151,119.00,214);
	// $("select").change(function () {
	// 	          var str = "";
	// 	          $("select option:selected").each(function () {
	// 	                str += $(this).text() + " ";
	// 	              });
	// 	          $("div").text(str);
	// 	        })
	// 	        .trigger('change');
	
	// spConfig.reloadPrice();
	// var checkboxes = getElementsByClassName("checkbox1");
	// if (undefined != checkboxes[0]){
		// jQuery("a.prices:last").click();
		// checkboxes[0].setAttribute("class", "checkbox active");
	// }
	// var selObj = document.getElementById('attribute975');
	 // if (undefined != selObj){
		// selObj.selectedIndex = 1;
		// 		selObj.options[1].selected = true;
		// spConfig.reloadPrice();
	 // }
	
//--------------------------------------------------------------------------//
//slider
    rotateNext = function(){    
		jQuery('.top-image-container .featured').filter(':visible').fadeOut(1000,function(){
			if(jQuery(this).next('div.featured').size()){
				jQuery(this).next().fadeIn(1000);
			}
			else{
				jQuery('.top-image-container .featured').eq(0).fadeIn(1000);
			}
		});
    };
	rotatePrev = function(){
		jQuery('.top-image-container .featured').filter(':visible').fadeOut(1000,function(){
			if(jQuery(this).prev('div.featured').size()){
				jQuery(this).prev().fadeIn(1000);
			} else{
				jQuery('.top-image-container .featured').eq(jQuery('.top-image-container .featured').size()-1).fadeIn(1000);
			}
		});
	};
    rotateStart = function(){      			//Rotation + Timing Event
        play = setInterval(function(){ 		//Set timer - this will repeat itself every 10 seconds
            rotateNext();
        }, 10000); 							//Timer speed in milliseconds (10 seconds)
    };
	nextSlider = function(){  
		clearInterval(play); 				//Stop the rotation
        	rotateNext(); 					//Trigger rotation immediately
            rotateStart(); 					// Resume rotation
            return false; 					//Prevent browser jump to link anchor
	};
	prevSlider = function(){  
		clearInterval(play); 				//Stop the rotation
        rotatePrev(); 						//Trigger rotation immediately
        rotateStart(); 						// Resume rotation
        return false; 						//Prevent browser jump to link anchor
	};
	
    rotateStart(); 							//Run function on launch

//--------------------------------------------------------------------------//
// popup window
    jQuery('a.popup[href^=#]').click(function() {
        var popID = jQuery(this).attr('rel');
        var popURL = jQuery(this).attr('href');
        var query = popURL.split('?');
        var dim = query[1].split('&');
        var popWidth = dim[0].split('=')[1];
        jQuery('#' + popID).fadeIn().css({
            'width': Number(popWidth)
        }).prepend('<a href="#" class="close"><img src="/skin/frontend/default/thefacepharmacy/css/images/buttons/button-close.png" class="btn_close" title="Close Window" alt="Close" /></a>');
        var popupLeftMargin = (((960 - jQuery('#' + popID).width()) / 2) - 60)
        var popMargTop = (jQuery('#' + popID).height()) / 2;
        var popMargLeft = (jQuery('#' + popID).width() - Number(popWidth) - 200) / 2;
        jQuery('#' + popID).css({
            'margin-top': -popMargTop,
            'margin-left': popupLeftMargin
        });
        jQuery('body').append('<div id="fade"></div>');
        //Add the fade layer to bottom of the body tag.
        jQuery('#fade').css({
            'filter': 'alpha(opacity=80)'
        }).fadeIn();
        //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies
        return false;
    });
	// close popup
    jQuery('a.close, #fade').live('click',
    function() {
        jQuery('#fade , .popup_block').fadeOut(function() {
            jQuery('#fade, a.close').remove();
        });
        return false;
    });
});

// if (jQuery('#advice-required-entry-attribute975')){
// 	// window.alert("Please select a size.");
// 	jQuery('#size-not-selected').fadeIn(1000);
// }



// .validation-advice
// #advice-required-entry-attribute975




// jQuery('.top-image-container .featured').filter(':visible').fadeOut(1000,function(){
// 		if(jQuery(this).next('div.featured').size()){
// 			jQuery(this).next().fadeIn(1000);
// 		}
// 		else{
// 			jQuery('.top-image-container .featured').eq(0).fadeIn(1000);
// 		}
// 	});

// function prevSlider() {
// 	jQuery('.top-image-container .featured').filter(':visible').fadeOut(1000,function(){
// 		if(jQuery(this).previous('div.featured').size()){
// 			jQuery(this).previous().fadeIn(1000);
// 		}
// 		else{
// 			jQuery('.top-image-container .featured').eq(0).fadeIn(1000);
// 		}
// 	});
// }


// JQuery("#top-image-prev").click(function () {
	// alert("yes");
	// top-image-button next
	// 	var $curr = $("#start");
	// 	$curr = $curr.next();
// });






// Alert for older/incompatible browsers
jQuery(document).ready(function() {
    jQuery.reject({
        reject: {
            all: false,
            // Covers Everything (Nothing blocked)
            msie5: true,
            msie6: true,
            msie7: true,
            // Covers MSIE 5-6 (Blocked by default)
            firefox1: true,
            firefox2: true,
            opera7: true,
            opera8: true,
            unknown: false,
            iphone: false
        },
        closeCookie: true
        // Set cookie to remmember close for this session
    });

    return false;
});



// jQuery(document).ready(function() {
// 	var x = document.getElementsByClassName('checkbox bundle-option-3 validate-one-required-by-name');for (var i=0;i<x.length;i++) {x[i].style.display = 'none';x[i].onclick = checkall;}function checkall(){var x = document.getElementsByClassName('checkbox bundle-option-3 validate-one-required-by-name');for (var i=0;i<x.length;i++) {x[i].checked = true}}
// });

// General Functions

function setSize(radio, price, parentId) {
	var selObj = document.getElementById('attribute975');

	for (var x = 0; x < selObj.length; x++) { 
	  if (selObj.options[x].value == radio) { 
	    selObj.options[x].selected = true; 
	  } 
	}
	resetCheckboxes();
    document.getElementById("checkbox" + radio).setAttribute("class", "checkbox active");
	spConfig.reloadPrice();
}

function resetCheckboxes() {
	var checkboxes = new Array();
	var checkbox;

	checkboxes = getElementsByClassName("checkbox");
	for (var i = 0; i < checkboxes.length; i++) {
		checkbox = checkboxes[i];
		checkbox.setAttribute("class", "checkbox");
	}
}


function getElementsByClassName( searchClass, domNode, tagName) { 
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) { 
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1) 
			el[j++] = tags[i];
	} 
	return el;
}

function setVisibility(objId, sVisibility) {
    var obj = document.getElementById(objId);
    obj.style.visibility = sVisibility;
}

function getScreen( url, size )
{
	if(url === null){ return ""; }
	size = (size === null) ? "big" : size;
	var vid;
	var results;
	results = url.match("[\\?&]v=([^&#]*)");
	vid = ( results === null ) ? url : results[1];
	if(size == "small"){
		return "http://img.youtube.com/vi/"+vid+"/2.jpg";
	}else {
		return "http://img.youtube.com/vi/"+vid+"/0.jpg";
	}
}







//bundle.changeSelection(this);
//JQuery("input[type='checkbox']:not([disabled='disabled'])").attr('checked', true);
//JQuery( "input[ @id ^= 'bundle-option' ]" ).text()
//$("td[id^='aa']")

//spConfig.reloadPrice();
