jQuery.noConflict();
jQuery(document).ready(function(){

    var getById = function(n){
        return document.getElementById(n);
    };
    function onlyAlphaNumericOld(s){

        var o = [], a = s.split();
        for(var i=0, l = s.length; i<l; i++){
            if(/[a-zA-Z0-9]/.test(s[i])){
                o.push(s[i]);
            } else {
                o.push('-');
            }
        }

        while( o.length && o[o.length-1] == '-' ) o.pop();

        return o.join('');
    }

    var me = this,
    getByTagName = function(n){
        return document.getElementsByTagName(n);
    },
    getById = function(n){
        return document.getElementById(n);
    },
    ie = jQuery.browser.msie,
    ie6 = ie && parseInt(jQuery.browser.version) == 6,
    giftCardBalanceURL = "SVSGiftCards-CheckBalance",
    svstotal = 0,
    estimatedtotal = 0;
    /// end var block

    //add className-s to <html> indicating
    // 1. that javascript is enabled (jsEnabled)
    // 2. if we are in IE, that we are (ie)
    // 3. if we are in IE, what version (ie6 or ie7 or..)
    if(jQuery.browser.safari) jQuery.browser.version = navigator.userAgent.replace(/.*?Version.([0-9]).*/,'$1');
    jQuery('html').addClass( 'jsEnabled' + ((jQuery.browser.msie ? ' ie ie': jQuery.browser.safari ? ' safari safari' : ' other') + parseInt(jQuery.browser.version)) );



    // MODALS
    jQuery.ui.dialog.defaults.bgiframe = true;
    jQuery.ui.dialog.defaults.modal = true;
    jQuery.ui.dialog.defaults.width = 367;
    jQuery.ui.dialog.defaults.draggable = false;
    jQuery.ui.dialog.defaults.resizable = false;
    var modals = {};
    function modal(o){
        var dialog = modals[o.title];
        //~ if(dialog){
        //~jQuery(dialog).dialog({modal:true});
        //~ } else {
        dialog = document.createElement('div');
        dialog.title = o.title;
        dialog.innerHTML = o.body;
        getById('page').appendChild(dialog);

        dialog.id = buildPopupDivId( o.title );
        // dialog.id = ('modal-' + (onlyAlphaNumeric(o.title)).toLowerCase());
        modals[o.title] =jQuery(dialog).dialog({
            modal:true,
            width:(o.width || jQuery.ui.dialog.defaults.width)
        });
        return modals;
    //~ }
    }

    me.modal = modal;

    // inserted 2009-03-12 by JN:
    // when representing the content of the popup, we need to know which ID
    // was used for the popup in the first place.  RF generated the ID
    // on the inline.  This exports the funtionality to create the ID.
    function buildPopupDivId( title )
    {
        return 'modal-' + onlyAlphaNumericOld( title ).toLowerCase();
    }

    /* wire up video overlay links */
    jQuery('#almass').click(function(){

        var videoUrl = this.rel.substr(6);
        //console.log(videoUrl);

        modal({
            title: 'Video'
            ,
            body: (
                '<div class="social-btns"><a href="#"><img class="social" src="images/video-player/social-email-link.gif"></a><a href="#"><img class="social" src="images/video-player/social-embed-link.gif"></a><img src="images/video-player/social-links-divider.gif"><a href="#"><img class="social" src="images/video-player/social-facebook-link.gif"></a><a href="#"><img class="social" src="images/video-player/social-delicious-link.gif"></a><a href="#"><img class="social" src="images/video-player/social-digg-link.gif"></a></div>'
                +'<div id="video-player-holder" style="height:360px"><div id="video-player"></div></div>'
                +'<div class="video-description"></div>'
                )
            ,
            width: 480
        });

        jQuery('.ui-dialog').addClass('video-player-dialog');

        $playerHolder = jQuery('#video-player-holder'); // based on the title!

        swfobject.embedSWF("../flash/player.swf", "video-player", "480", "360", "9.0.45", null, {
            vidToPlay:videoUrl
        }, {
            wmode:'opaque',
            quality:'high',
            menu:'false'
        }, {
            id:'videoPlayer',
            name:'videoPlayer'
        });

        return false;
    });

    /* Video player modal */
    jQuery('a.video-player-modal').click(function(){

        //var __url = this.attributes['url'].value;
        var __media = this.attributes['media'].value;
        
        var __videoname = ((this.attributes['videoname'] == null) ? '' : this.attributes["videoname"].value);
        var __title = ((this.attributes["title"] == null) ? "Video" : this.attributes["title"].value);
        var stylenum = '';

        if (this.attributes["stylenumber"] != null) {
            stylenum = this.attributes["stylenumber"].value;
        }
        modal({
            title: __title
            ,
            body: 'Loading Video...'
            ,
            width: 530
        });
        
        jQuery.ajax({
            type: "POST",
            url: '/2011primavara/prod_video.php',
            data: 'media=' + __media + '&videoname=' + __videoname + '&stylenum=' + stylenum,
            success: function(response){
                modals[__title].html(response);
            }
            ,
            error:function(){
                modals[__title].html(
                    '<div class="forgot-holder" id="confirmation-message">'
                    +'<h3>Error</h3>'
                    +'<p>There was a problem sending your request.</p>'
                    +'</div>'
                    );
            }
        });
        return false;
    });
    
        
    /* Video player modal */
    jQuery('a.video-player-modal-ext').click(function(){

        //var __url = this.attributes['url'].value;
        var __media = this.attributes['media'].value;                
        
        var so = new SWFObject("http://www.columbia-sportswear.ro/2011primavara/js/main_popup.swf", "flashobj", "480", "360", "9", "#FFFFFF");
        so.addParam("wmode", "transparent");
        var vidp = "videoPath=/2011primavara/explore/"+__media;
        so.addParam("flashVars", vidp);
        so.write("flashcontentmodal");
        
        //jQuery('#flashcontentmodal').html(so);
        
    });


});
