// requires jQuery 1.3 and SWFObject 2.2

(function() {
	// var videoSizes = {
	//     1:[230, 130],
	//     2:[470, 264],
	//     3:[710, 400],
	//     4:[950, 534]
	// }
	
	var adjustHeight = function(elems, sels) {
	    if (elems.length <= 1 || sels.length <= 0) { return }
        
        var highests = {};
        for (var i = 0; i < sels.length; i++) {
            highests[sels[i]] = 0;
        }
        
        for (var i = 0; i < elems.length; i++) {
            var $elem = $(elems[i]);
            for (var j = 0; j < sels.length; j++) {
                var sel = sels[j];
                var height = $($elem.children(sel)[0]).css("height");
                height = Number(/(\d*)/.exec(height)[1]);
                if (height > highests[sel]) {
                    highests[sel] = height;
                }
            }
        }
        
        for (var i = 0; i < elems.length; i++) {
            var $elem = $(elems[i]);
            for (var j = 0; j < sels.length; j++) {
                var sel = sels[j];
                $($elem.children(sel)[0]).css(
                    "height",
                    highests[sel].toString() + "px"
                );
            }
        }
	}
	
    // =====================
    // = scrweb Definition =
    // =====================
    window.scrweb = {
        init:function() {
            window.scrweb.engridLargeProducts();
            
            if (!$.browser.safari) {
                window.scrweb.dressText();
            }
        },
        dressText:function() {
            $('.artist-name').css('text-shadow', 'none');
            $('.artist-role').css('text-shadow', 'none');
            $('.product-name').css('text-shadow', 'none');
            $('.product-price').css('text-shadow', 'none').css('font-weight', 'normal');
        },
        charttab:function() {
            $('.product-charttab').each(function(index) {
                var targets = $(this).children('ul.product-charttabselector')
                                     .children('li')
                                     .children('a');
                targets.click(function() {
                    if ($(this).hasClass("selected")) {
                        return false;
                    } else {
                        var elem = this;
                        targets.each(function(index) {
                            var href = $(this).attr("href");
                            if (this === elem) {
                                $(this).addClass("selected");
                                $(href).css('display', 'block');
                            } else {
                                $(this).removeClass('selected');
                                $(href).css('display', 'none');
                            }
                        });
                        return false;
                    }
                });
                targets.each(function(index) {
                    var href = $(this).attr('href');
                    if ($(this).hasClass("selected")) {
                        $(href).css('display', 'block');
                    } else {
                        $(href).css('display', 'none');
                    }
                });
            });
        },
        randomTopProduct:function(index) {
            $.getJSON(
                '/assets/products.json',
                function(products){
                    var product = products[index];
                    
                    var html;
                    
                    // // vimeo
                    // html =
                    //       '<object width="950" height="534">'
                    //     +     '<param name="allowfullscreen" value="true" />'
                    //     +     '<param name="allowscriptaccess" value="always" />'
                    //     +     '<param'
                    //     +         ' name="movie"'
                    //     +         ' value="'
                    //     +             'http://vimeo.com/moogaloop.swf'
                    //     +             '?clip_id=' + product.vimeoid
                    //     +             '&amp;server=vimeo.com'
                    //     +             '&amp;show_title=0'
                    //     +             '&amp;show_byline=0'
                    //     +             '&amp;show_portrait=0'
                    //     +             '&amp;color=ffffff'
                    //     +             '&amp;fullscreen=1'
                    //     +             '&amp;autoplay=1" />'
                    //     +     '<embed'
                    //     +         ' src="'
                    //     +             'http://vimeo.com/moogaloop.swf'
                    //     +             '?clip_id=' + product.vimeoid
                    //     +             '&amp;server=vimeo.com'
                    //     +             '&amp;show_title=0'
                    //     +             '&amp;show_byline=0'
                    //     +             '&amp;show_portrait=0'
                    //     +             '&amp;color=ffffff'
                    //     +             '&amp;fullscreen=1'
                    //     +             '&amp;autoplay=1"'
                    //     +         ' type="application/x-shockwave-flash"'
                    //     +         ' allowfullscreen="true"'
                    //     +         ' allowscriptaccess="always"'
                    //     +         ' width="950"'
                    //     +         ' height="534">'
                    //     +     '</embed>'
                    //     + '</object>';
                        
                    // youtube
                    // html =
                    //       '<object width="950" height="559">'
                    //     +     '<param'
                    //     +         ' name="movie"'
                    //     +         ' value="http://www.youtube.com/v/'
                    //     +             product.youtubeid
                    //     +             '&amp;hl=ja'
                    //     +             '&amp;fs=1'
                    //     +             '&amp;rel=0'
                    //     +             '&amp;hd=1'
                    //     +             '&amp;autoplay=0'
                    //     +             '&amp;showinfo=0'
                    //     +             '&amp;enablejsapi=1">'
                    //     +     '</param>'
                    //     +     '<param name="allowFullScreen" value="true"></param>'
                    //     +     '<param name="allowscriptaccess" value="always"></param>'
                    //     +     '<embed'
                    //     +         ' src="http://www.youtube.com/v/'
                    //     +             product.youtubeid
                    //     +             '&amp;hl=ja'
                    //     +             '&amp;fs=1'
                    //     +             '&amp;rel=0'
                    //     +             '&amp;hd=1'
                    //     +             '&amp;autoplay=0'
                    //     +             '&amp;showinfo=0'
                    //     +             '&amp;enablejsapi=1"'
                    //     +         ' type="application/x-shockwave-flash"'
                    //     +         ' allowscriptaccess="always"'
                    //     +         ' allowfullscreen="true"'
                    //     +         ' width="950"'
                    //     +         ' height="559">'
                    //     +     '</embed>'
                    //     + '</object>';
                    // 
                    // $('.featuredproduct .product-preview').html(html);
                    
                    // youtube with apis
                    // $(".product-previewhiddencontainer").css("visibility", "hidden");
                    if ($.browser.msie) {
                        $(".product-previewhiddencontainer").css("visibility", "visible");
                    }
                    window.onYouTubePlayerReady = function(playerid) {
            			setTimeout(function() {
                            $(".product-previewhiddencontainer").css("visibility", "visible");
            				p.playVideo();
            			}, 1000);
                        var p = document.getElementById("ytPlayer");
                    }
                    swfobject.embedSWF(
                          "http://www.youtube.com/v/"
            			+ product.youtubeid
            			+ "&enablejsapi=1&playerapiid=player1&rel=0&hd=1&showinfo=0",
            			"productrandomvideo4",
            			"950px",
            			"559px",
            			"8",
            			null,
            			null,
            			{
            				allowScriptAccess:"always",
            				bgcolor:"#000000"
            			},
            			{
            				id:"ytPlayer"
            			}
                    );
                    
                    $('.featuredproduct .product-permalink').attr('href', '/products/' + product.uniquename + '/');
                    
                    $('.featuredproduct .product-name').text(product.name);
                    
                    $('.featuredproduct .product-artist').text("by " + product.artist.name);
                    
                    $('.featuredproduct .product-summary').html(product.summary);
                    
                    html = "";
                    
                    if (product.macosx) {
                        html +=
                              '<li class="product-os-macosx' + (product.windows ? '' : ' comp-lastchild') + '">'
                            +     '<span class="assistivetext">'
                            +         'Mac OS X'
                            +     '</span>'
                            + '</li>';
                    }
                    if (product.windows) {
                        html +=
                              '<li class="product-os-windows comp-lastchild">'
                            +     '<span class="assistivetext">'
                            +         'Windows'
                            +     '</span>'
                            + '</li>';
                    }
                    
                    $('.featuredproduct .product-oslist').html(html);
                    
                    if (product.price > 0) {
                        html = '<li>Free to Try</li>';
                        
                        html +=
                              '<li>'
                            +     product.pricestring + ' to Get License'
                            + '</li>';
                    } else {
                        html = '<li>Free Product</li>';
                    }
                    
                    $('.featuredproduct .product-price').html(html);
                    
                    $('.featuredproduct .product-moreinfo').attr("href", "/products/" + product.uniquename + '/');
                }
            );
        },
        embedChart:function() {
            $('.chart').each(function(index) {
                var elemID = $(this).attr('id');
                var src    = $(this).attr('src');
                var suffix = $(this).attr("suffix");
                
                if (src && elemID && suffix) {
                    swfobject.embedSWF(
                        "/assets/swf/graph.swf",
                        elemID,
                        "100%",
                        "100%",
                        "9.0.124",
                        "/assets/js/swfobject/expressInstall.swf",
                        {
                            url:src,
                            suffix:suffix
                        },
                        {
                            bgcolor:"#141414"
                        },
                        null
                    );
                }
            });
        },
        engridLargeProducts:function() {
            var cols = 2;
            var list = $('.productlist .largeproduct');
            var bottomIndex = list.length - ((list.length % cols) || cols);
            var groups = [[]];
            
            list.each(function(index) {
                if ((index + 1) % cols === 0) {
                    $(this).addClass("last");
                }
                if (index >= bottomIndex) {
                    $(this).addClass("listbottom");
                }
                
                var group = groups[groups.length - 1];
                if (group.length >= cols) {
                    groups.push(group = []);
                }
                group.push(this);
    	    });
    	    
    	    if ($.browser.msie) {
    	        return;
    	    }
    	    
            for (var i = 0; i < groups.length; i++) {
                adjustHeight(groups[i], [
                    '.product-titleinfo',
                    '.product-summary'
                ]);
            }
        }
    };
})();