//ROLLOVER

function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

window.onload = initRollovers;


//JQUERY POP-OUT	
		
jQuery(function ($) {
	// Load dialog on click
//	$('a').click(function (e) {e.preventDefault();
//		('#modal_' + this.id).modal({overlayClose:true, position: ['',], onOpen: function (dialog) {
//	dialog.overlay.fadeIn('slow', function () {
//		dialog.data.hide();
//		dialog.container.slideToggle('slow', function () {
//			dialog.data.slideDown('slow');
//		});
		
	
	
	
	$('.nav-bottom .basic').click(function (e) {
	    properParent = $(this).parents('div.box').get('0');
		slider = $(properParent).find('ul.slider').get('0');
		if (slider == undefined){
			properParent = $(this).parents('div.nav-bottom').get('0');
			currentLink = $(properParent).attr('data-modal');
		}else{
			currentSlide = $(slider).data('current_slide');
			currentLink = $($(slider).children().get(currentSlide)).attr('data-modal');
		}
		$('#'+currentLink+'-content').modal({
	        overlayClose: true,
	        position: ['', ],
	        onOpen: function (dialog) {
	            dialog.overlay.fadeIn('slow', function () {
	                dialog.data.hide();
	                dialog.container.slideToggle('slow', function () {
	                    dialog.data.slideDown('slow');
	                });


	            });
	        },
	        onClose: function (dialog) {
	            dialog.data.fadeOut('slow', function () {
	                dialog.container.hide('slow', function () {
	                    dialog.overlay.slideUp('slow', function () {
	                        $.modal.close();
	                    });
	                });
	            });
	        }
	    });
		e.preventDefault();

	});
	
	$('.expandable-content .basic').click(function (e) {
		properParent = $(this).parent().get('0');
		currentLink = $(this).attr('data-modal');
		$('#'+currentLink+'-content').modal({
	        overlayClose: true,
	        position: ['', ],
	        onOpen: function (dialog) {
	            dialog.overlay.fadeIn('slow', function () {
	                dialog.data.hide();
	                dialog.container.slideToggle('slow', function () {
	                    dialog.data.slideDown('slow');
	                });


	            });
	        },
	        onClose: function (dialog) {
	            dialog.data.fadeOut('slow', function () {
	                dialog.container.hide('slow', function () {
	                    dialog.overlay.slideUp('slow', function () {
	                        $.modal.close();
	                    });
	                });
	            });
	        }
	    });
		e.preventDefault();

	});
	
});

//CAROUSEL

(function($){
	$.fn.carousel = function(config) {
		var defaults = {
			slider: '.slider',
			slide: '.slide',
			prevSlide: '.prev',
			nextSlide: '.next',
			speed: 500
		},
		opt = $.extend(defaults, config),
		dStyle = document.body.style,
		transitionSupport = dStyle.webkitTransition !== undefined || 
				    dStyle.MozTransition !== undefined ||
				    dStyle.msTransition !== undefined ||
				    dStyle.OTransition !== undefined ||
				    dStyle.transition !== undefined;

		$(opt.prevSlide).addClass('disabled');
		$(opt.slider).data('current_slide',0);
		if( !!~location.search.indexOf("matmarquis.com") ) {
			window.location = "http://wil.to/_/whatno.gif";
			return;
		}


		function moveNext($slider) {
			var leftmargin = $slider.attr('style').match(/margin\-left:(.*[0-9])/i) && parseInt(RegExp.$1),
				$slide = $slider.find(opt.slide);
			if (!$slider.is(":animated") && (-leftmargin) != (($slide.length - 1) * 100)) {
				leftmargin -= 100;

				if(transitionSupport) {
					$slider.css('marginLeft', leftmargin + "%");
				} else {
					$slider.animate({ marginLeft: leftmargin + "%" }, opt.speed);
				}
				if((-leftmargin) == ($slide.length - 1) * 100) {
					$slider.data('current_slide',-leftmargin/100);
					return false;
				}
			}
			$slider.data('current_slide',-leftmargin/100);
		}

		function movePrev($slider) {
			var leftmargin = $slider.attr('style').match(/margin\-left:(.*[0-9])/i) && parseInt(RegExp.$1);
			if(!$slider.is(":animated") && (leftmargin != 0)) {
				leftmargin += 100;

				if(transitionSupport) {
					$slider.css('marginLeft', leftmargin + "%");
				} else {
					$slider.animate({ marginLeft: leftmargin + "%" }, opt.speed);
				}					
				if(leftmargin == 0) {
					$slider.data('current_slide',-leftmargin/100);
					return false;
				}
			}
			$slider.data('current_slide',-leftmargin/100);
		}

		$(opt.nextSlide + ',' + opt.prevSlide).click(function(e) {
			var $el = $(this),
				link = $el.attr('href'),
				$target = $(opt.slider).filter(link);
				
				if ($target.attr('id') != undefined) {
					$(opt.nextSlide).each(function(){
						if ($(this)[0] == $el[0]) {
							if (moveNext($target) === false) {
								$el.addClass('disabled');
							};
							$(opt.prevSlide).filter(function(){
								return this.getAttribute('href') === link;
							}).removeClass('disabled');
						}
					});
					
					$(opt.prevSlide).each(function(){
						if ($(this)[0] == $el[0]) {
							if (movePrev($target) === false) {
								$el.addClass('disabled');
							};
							$(opt.nextSlide).filter(function(){
								return this.getAttribute('href') === link;
							}).removeClass('disabled');
						}
					});
				}
			e.preventDefault();
		});


		//swipes trigger move left/right
		$(this).live( "swipe", function(e, ui){
			(ui.direction === "left" ? moveNext : movePrev)($(this).find( opt.slider ));
		});

		return this.each(function() {
			var $wrap = $(this),
				$slider = $wrap.find(opt.slider),
				$slide = $wrap.find(opt.slide),			
				slidenum = $slide.length,
				speed = opt.speed / 1000;

			$wrap.css({
				overflow: "hidden"
			});
			
			$slider.css({
				marginLeft: "0px",
				float: "left",
				width: 100 * slidenum + "%",
				"-webkit-transition": "margin-left " + speed + "s ease",
				"-moz-transition": "margin-left " + speed + "s ease",
				"-ms-transition": "margin-left " + speed + "s ease",
				"-o-transition": "margin-left " + speed + "s ease",
				"transition": "margin-left " + speed + "s ease"
			});	
				    
			$slide.css({
				float: "left",
				width: (100 / slidenum) + "%"				
			});		
		});
	};
	
	
	//modified swipe events from jQuery Mobile
	// also handles swipeleft, swiperight
	$.event.special.swipe = {
		setup: function() {
			var $el = $(this);
			
			$el
				.bind("touchstart", function(e) {
					var data = e.originalEvent.touches ? e.originalEvent.touches[0] : e,
						start = {
							time: (new Date).getTime(),
							coords: [ data.pageX, data.pageY ],
							origin: $(e.target)
						},
						stop;
					
					function moveHandler(e) {
						if(!start) {
							return;
						}
						
						var data = e.originalEvent.touches ? e.originalEvent.touches[0] : e;
						stop = {
								time: (new Date).getTime(),
								coords: [ data.pageX, data.pageY ]
						};
						
						// prevent scrolling
						if (Math.abs(start.coords[0] - stop.coords[0]) > 10) {
							e.preventDefault();
						}
					}
					
					$el
						.bind("touchmove", moveHandler)
						.one("touchend", function(e) {
							$el.unbind("touchmove", moveHandler);
							if (start && stop) {
								if (stop.time - start.time < 1000 && 
										Math.abs(start.coords[0] - stop.coords[0]) > 30 &&
										Math.abs(start.coords[1] - stop.coords[1]) < 75) {
										var left = start.coords[0] > stop.coords[0];
									start.origin
										.trigger("swipe", {direction: left ? "left" : "right"})
										.trigger(left ? "swipeleft" : "swiperight" );
								}
							}
							start = stop = undefined;
						});
				});
		}
	};
})(jQuery);


/* MASONRY FILTER EFFECT 
 $('a.filter').click(function(){
  filterBoxes(this.id);
 })
   
 function filterBoxes(klass){
  if (klass == "all") { klass = "box" }
  $('#holder').find('.' + klass)
   .hide()
   .appendTo('#content')
   .fadeIn('400')
  $('#content').find('.box:not(.' + klass + ')')
   .fadeOut( '400', function(){
    $(this).appendTo('#holder') ;
   });
  setTimeout(function(){ $('#content').masonry() },500);
 }*/ 

/* EXPANDABLE CONTENT */ 

 $('.expand h2.trigger').click(function () {
        if ($(this).parent().is('.expanded')) {
            restoreBoxes();
        } else {
            var size = ($(this).parent().attr('data-size')) ? $(this).parent().attr('data-size').split(',') : defaultSize;
            $(this).parent()
            // save original box size
            .data('size', [$(this).parent().width(), $(this).parent().height()]).animate({
                width: size[0],
                height: size[1]
            }, function () {
                // show hidden content when box has expanded completely
   $(this).find('.expandable').show('normal')


                $(this).find('.hideable').hide('normal');
                $('#content').masonry();
            });
            restoreBoxes();
            $(this).parent().addClass('expanded');
        }

        function restoreBoxes() {
            var len = $('.expanded').length - 1;
            $('.expanded').each(function (i) {
                var box = $(this).data('size');
                $(this).find('.expandable').hide('normal');
                $(this).find('.hideable').show('normal');
                $(this).animate({
                    width: (box[0] || 470),
                    height: (box[1] || 'auto')
                }, function () {
                    if (i >= len) {
                        $('#content').masonry();
                    }
                }).removeClass('expanded');
            })
        }
    });
 function onLoadExpand (element) {
        if ($(element).parent().is('.expanded')) {
            restoreBoxes();
        } else {
            var size = ($(element).parent().attr('data-size')) ? $(element).parent().attr('data-size').split(',') : defaultSize;
            $(element).parent()
            // save original box size
            .data('size', [$(element).parent().width(), $(element).parent().height()]).animate({
                width: size[0],
                height: size[1]
            }, function () {
                // show hidden content when box has expanded completely
			$(this).find('.expandable').show('normal')


                $(this).find('.hideable').hide('normal');
                $('#content').masonry();
            });
            restoreBoxes();
            $(element).parent().addClass('expanded');
        }

        function restoreBoxes() {
            var len = $('.expanded').length - 1;
            $('.expanded').each(function (i) {
                var box = $(this).data('size');
                $(this).find('.expandable').hide('normal');
                $(this).find('.hideable').show('normal');
                $(this).animate({
                    width: (box[0] || 470),
                    height: (box[1] || 'auto')
                }, function () {
                    if (i >= len) {
                        $('#content').masonry();
                    }
                }).removeClass('expanded');
            })
        }
    };
(function($) {
	$.QueryString = (function(a) {
		if (a == "") return {};
		var b = {};
			for (var i = 0; i < a.length; ++i){
				var p=a[i].split('=');	
				if (p.length != 2) continue;
				b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
			}
		return b;
	})(window.location.search.substr(1).split('&'))
})(jQuery);

	

