jQuery(document).ready(function() {
  $.extend($.ui.accordion.animations, {
    slowslide: function(options) { $.ui.accordion.animations.slide(options, { duration: 1000 }); }
  });

  $('#products_accordion').accordion( { header: 'h2', collapsible: true, active: false, autoHeight: false, changestart: function(event, ui) {ui.newHeader.css('background','url(http://torchmate.com/images/uploads/template/accordion_bg_active.jpg) no-repeat top'); }, change: function(event, ui) {ui.oldHeader.css('background','url(http://torchmate.com/images/uploads/template/accordion_bg.jpg) no-repeat top'); } } );
  $('#products_accordion h2:first').css('background','url(http://torchmate.com/images/uploads/template/accordion_bg_active.jpg) no-repeat top');
  $('#products_accordion h2').hover(function() {$(this).css('text-decoration','underline');},function() {$(this).css('text-decoration','none');});
  setTimeout(function() { $('#products_accordion').accordion( 'activate', 0 ); }, 500);

  $('#accordion').accordion( { header: 'h1', collapsible: true, active: false, clearStyle: true, autoHeight: false } );
  $('.specifications').accordion( { header: 'h2', collapsible: true, active: false, animated: 'slowslide', autoHeight: false } );

  $('#accordion h1').hover(function() {$(this).css('text-decoration','underline');},function() {$(this).css('text-decoration','none');});
  //$('.specifications h2').click(function() { $(this).next().toggle(); $('#accordion').accordion( 'resize' ); return false; }).next().hide();
  $('.specifications h2').hover(function() {$(this).css('text-decoration','underline');},function() {$(this).css('text-decoration','none');});
  $('#accordion_h2').accordion( { header: 'h2', collapsible: true, active: false, animated: 'slowslide', clearStyle: true, autoHeight: false } );
  $('#accordion_h2 h2').hover(function() {$(this).css('text-decoration','underline');},function() {$(this).css('text-decoration','none');});

  if( document.location.hash == '#kits_details' && (document.location.pathname == '/index.php/products/torchmate_2/' || document.location.pathname == '/products/torchmate_2/') ) {
    $('#accordion').accordion( 'activate', 1 );
  } else if( document.location.hash == '#kits_details' && (document.location.pathname == '/index.php/products/torchmate_3/' || document.location.pathname == '/products/torchmate_3/') ) {
    $('#accordion').accordion( 'activate', 1 );
  } else if( document.location.hash == '#finished' && (document.location.pathname == '/index.php/products/torchmate_x/' || document.location.pathname == '/products/torchmate_x/') ) {
    $('#accordion').accordion( 'activate', 1 );
  } else if( document.location.hash == '#torchmate_x' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 0 );
  } else if( document.location.hash == '#torchmate_3' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 1 );
  } else if( document.location.hash == '#torchmate_2' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 2 );
  } else if( document.location.hash == '#torchmate_1' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 3 );
  } else if( document.location.hash == '#torchmate_2x2' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 4 );
  } else if( document.location.hash == '#plate_marker_attachment' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 5 );
  } else if( document.location.hash == '#tube_cutting_attachment' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 6 );
  } else if( document.location.hash == '#robot' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 7 );
  } else if( document.location.hash == '#router' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 8 );
  } else if( document.location.hash == '#plasma_cutter' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 9 );
  } else if( document.location.hash == '#miscellaneous' && (document.location.pathname == '/index.php/resource_center/demo_videos/' || document.location.pathname == '/resource_center/demo_videos/') ) {
    $('#accordion').accordion( 'activate', 10 );
  } else {
    setTimeout(function() { $('#accordion').accordion( 'activate', 0 ); }, 500);
  }

  setTimeout(function() { $('#accordion_h2').accordion( 'activate', 0 ); }, 500);

  $('#how_to_choose input:checkbox').click(function(){
        if($.browser.msie){
            if( $(this).is(':checked') ) getAvailableProductsDisable(); else getAvailableProductsEnable();
        }
  }); 
  $('#how_to_choose input:checkbox').change( function () { if( $(this).is(':checked') ) getAvailableProductsDisable(); else getAvailableProductsEnable(); } );
});

// IE5.5+ PNG Alpha Fix v2.0 Alpha: Background Tiling Support
// (c) 2008-2009 Angus Turnbull http://www.twinhelix.com

// This is licensed under the GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/

var IEPNGFix = window.IEPNGFix || {};

IEPNGFix.tileBG = function(elm, pngSrc, ready) {
	// Params: A reference to a DOM element, the PNG src file pathname, and a
	// hidden "ready-to-run" passed when called back after image preloading.

	var data = this.data[elm.uniqueID],
		elmW = Math.max(elm.clientWidth, elm.scrollWidth),
		elmH = Math.max(elm.clientHeight, elm.scrollHeight),
		bgX = elm.currentStyle.backgroundPositionX,
		bgY = elm.currentStyle.backgroundPositionY,
		bgR = elm.currentStyle.backgroundRepeat;

	// Cache of DIVs created per element, and image preloader/data.
	if (!data.tiles) {
		data.tiles = {
			elm: elm,
			src: '',
			cache: [],
			img: new Image(),
			old: {}};
	}
	var tiles = data.tiles,
		pngW = tiles.img.width,
		pngH = tiles.img.height;

	if (pngSrc) {
		if (!ready && pngSrc != tiles.src) {
			// New image? Preload it with a callback to detect dimensions.
			tiles.img.onload = function() {
				this.onload = null;
				IEPNGFix.tileBG(elm, pngSrc, 1);
			};
			return tiles.img.src = pngSrc;
		}
	} else {
		// No image?
		if (tiles.src) ready = 1;
		pngW = pngH = 0;
	}
	tiles.src = pngSrc;

	if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
		bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {return;}

	// Convert English and percentage positions to pixels.
	var pos = {
			top: '0%',
			left: '0%',
			center: '50%',
			bottom: '100%',
			right: '100%'
		},
		x,
		y,
		pc;
	x = pos[bgX] || bgX;
	y = pos[bgY] || bgY;
	if (pc = x.match(/(\d+)%/)) {
		x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
	}
	if (pc = y.match(/(\d+)%/)) {
		y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
	}
	x = parseInt(x);
	y = parseInt(y);

	// Handle backgroundRepeat.
	var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
		repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
	if (repeatX) {
		x %= pngW;
		if (x > 0) x -= pngW;
	}
	if (repeatY) {
		y %= pngH;
		if (y > 0) y -= pngH;
	}

	// Go!
	this.hook.enabled = 0;
	if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
		elm.style.position = 'relative';
	}
	var count = 0,
		xPos,
		maxX = repeatX ? elmW : x + 0.1,
		yPos,
		maxY = repeatY ? elmH : y + 0.1,
		d,
		s,
		isNew;
	if (pngW && pngH) {
		for (xPos = x; xPos < maxX; xPos += pngW) {
			for (yPos = y; yPos < maxY; yPos += pngH) {
				isNew = 0;
				if (!tiles.cache[count]) {
					tiles.cache[count] = document.createElement('div');
					isNew = 1;
				}
				var clipR = Math.max(0, xPos + pngW > elmW ? elmW - xPos : pngW),
					clipB = Math.max(0, yPos + pngH > elmH ? elmH - yPos : pngH);
				d = tiles.cache[count];
				s = d.style;
				s.behavior = 'none';
				s.left = (xPos - parseInt(elm.currentStyle.paddingLeft)) + 'px';
				s.top = yPos + 'px';
				s.width = clipR + 'px';
				s.height = clipB + 'px';
				s.clip = 'rect(' +
					(yPos < 0 ? 0 - yPos : 0) + 'px,' +
					clipR + 'px,' +
					clipB + 'px,' +
					(xPos < 0 ? 0 - xPos : 0) + 'px)';
				s.display = 'block';
				if (isNew) {
					s.position = 'absolute';
					s.zIndex = -999;
					if (elm.firstChild) {
						elm.insertBefore(d, elm.firstChild);
					} else {elm.appendChild(d);}
				}
				this.fix(d, pngSrc, 0);
				count++;
			}
		}
	}
	while (count < tiles.cache.length) {
		this.fix(tiles.cache[count], '', 0);
		tiles.cache[count++].style.display = 'none';
	}

	this.hook.enabled = 1;

	// Cache so updates are infrequent.
	tiles.old = {
		w: elmW,
		h: elmH,
		x: bgX,
		y: bgY,
		r: bgR
	};
};


IEPNGFix.update = function() {
	// Update all PNG backgrounds.
	for (var i in IEPNGFix.data) {
		var t = IEPNGFix.data[i].tiles;
		if (t && t.elm && t.src) {
			IEPNGFix.tileBG(t.elm, t.src);
		}
	}
};
IEPNGFix.update.timer = 0;

if (window.attachEvent && !window.opera) {
	window.attachEvent('onresize', function() {
		clearTimeout(IEPNGFix.update.timer);
		IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
	});
}
if(location.href.indexOf("photo_gallery") != -1) {
var flickr = function($) {
			var photos = [];
			var numPhotos = 0;
			var numLoaded = 0;
			var index = 0;
			var loading = true;
			var locked = true;
			var flickrAPIKey = 'a762141cd7f560f209d45cf052cf1f95';
			$.getJSON("http://api.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&user_id=35781454@N04&api_key="+flickrAPIKey+"&name=value&format=json&jsoncallback=?", function(data){
				numPhotos = data.photos.photo.length;
				photos = [];
				for(var i=0; i<Math.min(20, numPhotos); i++) {
					photos.push(new Array());
					photos[i]['link'] = 'http://www.flickr.com/photos/torchmate/'+data.photos.photo[i].id+'/';
					(function(i) {
						$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key="+flickrAPIKey+"&photo_id="+data.photos.photo[i].id+"&format=json&jsoncallback=?", function(photo){
							var j = 3;
							while((photo.sizes.size[j].width > 500 || photo.sizes.size[j].height > 250) && j > 0)
								j--;
							photos[i]['src'] = photo.sizes.size[j].source;
							photos[i]['width'] = photo.sizes.size[j].width;
							photos[i]['height'] = photo.sizes.size[j].height;
							$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key="+flickrAPIKey+"&photo_id="+data.photos.photo[i].id+"&format=json&jsoncallback=?", function(info){
								photos[i]['caption'] = info.photo.description._content;
								numLoaded++;
								if(photos.length == numLoaded) {
									loading = false;
									$(document).ready(function(e) {
										locked = false;
										$('#flickr .inner').html('<div class="frame"><a href="'+photos[index]['link']+'" target="_blank"><img src="'+photos[index]['src']+'" /><br />'+photos[index]['caption']+'</a></div>');
									});
								}
							});
						});
					})(i);
				}
			});
			$(document).ready(function() {
			$('#flickr_sets a').click(function(e) {
				loading = true;
				var setID = $(this).attr('href').split('/');
				setID = setID[setID.length-2];
				$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&photoset_id="+setID+"&api_key="+flickrAPIKey+"&name=value&format=json&jsoncallback=?", function(data){
//					if(console)
//						console.log("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&photoset_id="+setID+"&api_key="+flickrAPIKey+"&name=value&format=json&jsoncallback=?");
					numPhotos = data.photoset.photo.length;
					numLoaded = 0;
					photos = [];
					for(var i=0; i<Math.min(20, numPhotos); i++) {
						photos.push(new Array());
						photos[i]['link'] = 'http://www.flickr.com/photos/torchmate/'+data.photoset.photo[i].id+'/';
						(function(i) {
							$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key="+flickrAPIKey+"&photo_id="+data.photoset.photo[i].id+"&format=json&jsoncallback=?", function(photo){
//								if(console)
//									console.log("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key="+flickrAPIKey+"&photo_id="+data.photoset.photo[i].id+"&format=json&jsoncallback=?");
								var j = 3;
								while((photo.sizes.size[j].width > 500 || photo.sizes.size[j].height > 250) && j > 0)
									j--;
								photos[i]['src'] = photo.sizes.size[j].source;
								photos[i]['width'] = photo.sizes.size[j].width;
								photos[i]['height'] = photo.sizes.size[j].height;
								$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key="+flickrAPIKey+"&photo_id="+data.photoset.photo[i].id+"&format=json&jsoncallback=?", function(info){
//									if(console)
//										console.log("http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key="+flickrAPIKey+"&photo_id="+data.photoset.photo[i].id+"&format=json&jsoncallback=?");
									photos[i]['caption'] = info.photo.description._content;
									numLoaded++;
									if(photos.length == numLoaded) {
										loading = false;
										$(document).ready(function(e) {
											locked = false;
											$('#flickr .inner').html('<div class="frame"><a href="'+photos[index]['link']+'" target="_blank"><img src="'+photos[index]['src']+'" /><br />'+photos[index]['caption']+'</a></div>');
										});
									}
								});
							});
						})(i);
					}
				});
				return false;
			});
			});
			return {
				previousPhoto: function() {
					this.gotoPhoto((index == 0) ? photos.length-1 : index-1);
				},
				nextPhoto: function() {
					this.gotoPhoto((index == photos.length-1) ? 0 : index+1);
				},
				gotoPhoto: function(i) {
					if(!locked && !loading) {
						locked = true;
						if(index < i && (index != 0 || i != photos.length-1) || i == 0 && index == photos.length-1) {
						//next
							$('#flickr .inner').append('<div class="frame"><a href="'+photos[i]['link']+'" target="_blank"><img src="'+photos[i]['src']+'" /><br />'+photos[i]['caption']+'</a></div>');
							$('#flickr .inner').animate({ left: -500 }, { duration: 500, queue: false, complete: function() {
								$('#flickr .inner .frame:eq(0)').remove();
								$(this).css('left', 0);
								locked = false;
							}});
						} else if(index > i || i == photos.length-1 && index == 0) {
						//previous
							$('#flickr .inner').css('left', -500).prepend('<div class="frame"><a href="'+photos[i]['link']+'" target="_blank"><img src="'+photos[i]['src']+'" /><br />'+photos[i]['caption']+'</a></div>');
							$('#flickr .inner').animate({ left: 0 }, { duration: 500, queue: false, complete: function() {
								$('#flickr .inner .frame:eq(1)').remove();
								locked = false;
							}});
						} else
							locked = false;
						index = i;
					}
				}
			};
		}(jQuery);
}
